In the fist case, i'm declaring a class with one atribute (UILabel *label) and later, i'm declaring the properties for that label.
No you are not. In the first case, you are declaring an instance variable called label
and a pair of accessor methoods called -setLabel:
and -label
(known together as a property). You have established no link between the property and the instance variable. They are at this point independent entities.
If you do this in the implementation:
@synthesize label = fooBar;
You are saying that the methods of the label property actually use a completely different instance variable to back the property.
I always thought I should declare class attributes
I used to think the same, but actually, if you are synthesizing the property, there's no point in declaring an ivar separately because the @synthesize will do it for you (and in ARC will apply the correct ownership qualifiers). I now do something like this:
@synthesize label = label_;
so I don't use the instance variable when I mean to use the property. e.g. [label length]
flags an error when I meant [[self label] length]
Also, if you change the implementation of the property to not use an instance variable, if you haven't declared the instance variable explicitly, it will go away and accidental uses of it (+ those in init
and dealloc
) will be flagged as errors.