2

I'm starting to learn Objective-C and I have a question:

When you declare the properties of a class, what is the difference between doing this?

FIRST CASE:

@interface ViewController : UIViewController 
{
 UILabel *label;
}
@property(nonatomic,retain) UILabel *label;
@end

SECOND CASE:

@interface ViewController : UIViewController 
{

}
@property(nonatomic,retain) UILabel *label;

@end

In the first case, I'm declaring a class with one attribute (UILabel *label) and later, I'm declaring the properties for that label.

In the second case, I only declare the properties. I always thought I should declare class attributes.

JeremyP
  • 84,577
  • 15
  • 123
  • 161
dpbataller
  • 1,197
  • 4
  • 12
  • 23
  • 2
    possible duplicate of [Property Declaration and Automatic Backing Storage Allocation](http://stackoverflow.com/questions/3238009/property-declaration-and-automatic-backing-storage-allocation) – outis May 31 '12 at 09:59

2 Answers2

2

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.

JeremyP
  • 84,577
  • 15
  • 123
  • 161
  • I think I have a problem of concepts. Our teacher of Java, has taught us that classes have attributes and methods. So I thought it UILabel * label would be a attribute of class ViewController , How should I call? Instance variables or attributes? – dpbataller May 31 '12 at 12:00
  • @dpbataller it is a matter of terminology I think. Variables attached to an instance of a class are called "instance variables". Methods are called "methods". There are no "class" variables but there are instance methods and class methods. With instance methods, the receiver (self inside the method) is an instance of the class. With class methods, the receiver (self) is the object that represents the class itself. – JeremyP May 31 '12 at 13:29
0

Another difference that has not been mentioned:

In your first case, label has protected access. Instance variables are by default @protected if not declared otherwise.

On the other hand, in the second case, label is private (at least in my compiler).

This difference matters when you have subclasses.

user102008
  • 30,736
  • 10
  • 83
  • 104