Assume that I have this piece of code:
@interface Foo : NSObject {
Bar *bar;
}
@property (retain, nonatomic) Bar *bar;
@end
When using this field/property, is there any difference between lines:
[self.bar doStuff];
and
[bar doStuff];
?
When doing assignment, property method will perform correct retaining, but what about the read access to the property, as described above? Is there any difference?
There is a big difference.
[self.bar doStuff]is equivalent to[[self bar] doStuff][bar doStuff]is equivalent to[self->bar doStuff]The former uses the accessor method, the latter just accesses the instance variable bar directly.
If you use the
@synthesizedirective on yourbarproperty, the compiler will generate two methods for you:Also note, that the compiler generated setter method is retaining your
Barinstance as you told it in the@propertydeclaration.