Geoff
Geoff

Reputation: 345

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

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):

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.

Upvotes: 1

Views: 360

Answers (1)

Jim
Jim

Reputation: 73936

  • 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.

Upvotes: 1

Related Questions