Posting an ObjectiveC beginner level question. When I used properties to declare objects I see that we can access a particular property by 2 methods.
@property(nonatomic,retain) NSString *str;
Use
@synthesize propertynameeg:@synthesize str;By using keyword
selfeg:self.str;
So what is the difference between these 2 methods and which is more suitable. Thanks for your time
@synthesizecan not be used to access the property. It is a compiler directive. When you declare a property using using@property, the accessor methods (getter and setter) are automatically generated by the compiler. In the older versions, you had to explicitly use@synthezieto let the compiler know that it has to generate the accessor methods. With the newer versions, it is not required. The compiler automatically generates the accessor methods.If you have declared the property as
And if don't use @synthesize, then the ivar will be _str and the getter will be
and the setter will be
If you have mentioned the @synthesize specifically as
Then the ivar will be
_mystrinstead of_str.So inorder to access the property str, you have to use
self.stror[self str]