开发者

Property syntax, Am I accessing the ivar directly or going through the getter/setter?

开发者 https://www.devze.com 2023-03-16 18:51 出处:网络
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.

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消