Here is my code
Class.h
@interface Class : NSObject
{
NSString *str;
}
@property (nonatomic, copy) NSString *str;
@end
@implementation Class
@synthesize str = _str;
-(void)someMethod
{
self.str = @"This is a string";
}
Here I can’t figure out does self.str access str ivar directly or by getter and setter methods “generated” by synthesize directive ?
If you use
self.str = …it’s just syntatactic sugar around[self setStr:…]. So you are going through the setter method. Even if you get a value withself.stryou are going through an accessor – which is useful to know if you are implementing lazily loaded properties.you can only access the iVar directly in your case with
_strbecause you’ve (correctly, In My Opinion) declared that to be the name of the backing store.Edited to add
There is a problem with your example – you’ve defined the iVar
strwhich isn’t being used (the iOS uses a modern runtime where you don’t need to declare iVars for properties that you synthesize). So although your code is writing to a backing store_strand that is the store that is being used throughself.strif you were to access thestrvariable directly you would be using the declared iVar, not the one that you have a property for.