I have seen a lot of code like this
header.h
@interface Foo : NSObject
{
NSString *str;
}
@property(nonatomic, retain) NSString *str;
@end
and then in implementation
@implementation Foo
@synthesize str = _str;
@end
I can’t understand what is the benefit of using such assignment ?
@synthesize str = _str;
It is just a common naming convention.
It is so that in your implementation, you can distinguish accessing a variable directly against accessing via the property accessor.
If you try and access str in your code, like [str length], the code won’t compile. You either need to do [self.str length] or [_str length].
… and as it’s an NSString immutable property, use
copy, notretain.