method returning wrong variable
This class I'm working with has three instance variables I'm interested in, NSmutablearrays xArray yArray and zArray, there are also other NSmutablearrays, a, bArray and c. I just added accessors
开发者_JS百科- (NSMutableArray *) xArray {
return xArray;
}
ditto for y and z. However, it's returning yArray, zArray and bArray for some reason. Why is that happening?
Okay, I just changed the names of the methods to GetXArray and everything seems to be returning the right variables. Now I'm really confused. How the heck did this happen and how do I prevent this from happening in the future?
This is fairly straight forward setting/getter stuff:
XYZArray.h
#import <Cocoa/Cocoa.h>
@interface XYZArray : NSObject {
NSMutableArray *x;
NSMutableArray *y;
NSMutableArray *z;
}
-(NSMutableArray*) x;
-(NSMutableArray*) y;
-(NSMutableArray*) z;
-(void) setX:(NSMutableArray*)newArray;
-(void) setY:(NSMutableArray*)newArray;
-(void) setZ:(NSMutableArray*)newArray;
@end
XYZArray.m
#import "XYZArray.h"
@implementation XYZArray
- (id)init
{
if((self = [super init]))
{
x = [[NSMutableArray alloc] init];
y = [[NSMutableArray alloc] init];
z = [[NSMutableArray alloc] init];
}
return self;
}
- (void) dealloc
{
[x release];
[y release];
[z release];
[super dealloc];
}
-(NSMutableArray*) x
{
return x;
}
-(NSMutableArray*) y
{
return y;
}
-(NSMutableArray*) z
{
return z;
}
-(void) setX:(NSMutableArray*)newArray
{
[newArray retain];
[x release];
x = newArray;
}
-(void) setY:(NSMutableArray*)newArray
{
[newArray retain];
[y release];
y = newArray;
}
-(void) setZ:(NSMutableArray*)newArray
{
[newArray retain];
[z release];
z = newArray;
}
@end
Better still, if you are using Objective-C 2.0 you can do all of this with properties and not write any code at all. Read this for an introduction, http://macdevelopertips.com/objective-c/objective-c-properties-setters-and-dot-syntax.html.
精彩评论