I am new to objective-c and am a little confused as to what I am accessing by when calling a property various ways in code.
// MyClass.h
@interface MyClass : NSObject {
}
@property ( nonatomic, retain ) NSString *name;
@end
//MyClass.m
#import "MyClass.h"
@implementation MyClass
@synthesize name;
// other code...
@end
I am unclear whether I am accessing the backing ivar or going through the getter and setter in using the following syntax (I will include my assumptions as to what I think it's doing):
name = @"Geoff";
is this going through the property setter or setting the ivar directly?self.name = @"Geoff";
going through the setter开发者_运维知识库self->name = @"Geoff;"
direct ivar access[ name release ];
is this accessing the ivar directly or going through the getter?
I know this can be disambiguated by setting the ivar in the synthesize statement like: @synthesize name=_name
as is done in a lot of the XCode 4 IOS templates.
name = @"Geoff";
is setting the ivar directly.[ name release ];
is accessing the ivar directly.
If you don't see self.
and you aren't calling a method to get or set the variable, then you are accessing the ivar.
For more details, see The Objective-C Programming Language.
精彩评论