views:

1210

answers:

4

Hi,

Whats the difference between a property and an instance variable in Objective-C. I need to understand this in OOP terms. Is a property declaration just a convenience wrapper (with @synthesize in the implementation) for accessing instance variables?

thanks,

codecowboy.

+4  A: 

I think you are pretty much there. The @property and @synthesize make the accessor declarations and implementation for the already declared ivar. You have various attributes you can define on the @property too giving you control over how it is generated to make it appropriate for the ivar

Have a look at "Objective C 2.0 Declared Properties"

Kevin
+2  A: 

Properties and ivars are two completely different things.

And instance variable is a variable stored inside the object, so each instance has its own. It is referenced by pointer addition relative to the object pointer/self (slightly indirected for the modern runtime, but functionally equivalent). ivars are generally internal to a class, and by default can only be accessed by the class and its descendents (@protected). Within methods they are available with no qualification, otherwise they can (but rarely are, ad usuaually should not) be accessed via indirection, eg obj->ivar.

A property defines a getter and setter (the setter is optional) interface. That's all it does. It defines two public methods:

- (TYPE) propname;
- (void) setPropname: (TYPE) newPropname;

These are defined as methods exactly as if you declared them like that, no more, no less. These methods are called either with the normal syntax ([obj propname] and [obj setPropname:n] or using the modern dot notation (obj.propname or obj.propname = n). These two options are syntactically different only, they behave identically, and you can use dot notation whether the methods are declared with @property or declared manually as above.

You must then implement the methods in the implementation, either by writing the methods yourself, by using @synthesize, or by handling the missing method dynamically.

Properties may be backed by an ivar (named the same or named differently (my preference to avoid confusion)), or they may not. They may store their value elsewhere, or they may calculate it from other data.

For example, you might have:

@property (nonatomic, readonly) NSString* fullname;

and then implement - (NSString*) fullname to return the concatenation of firstname and lastname.

Peter N Lewis
This is exactly where my confusion lies. Doesn't an object instance also own its own properties? My understanding of objects in general terms is that they contain information about state (data) and behaviour (methods). Why do I need instance variables AND properties? Are the instance variables immutable unless I declare them as properties?
codecowboy
A property is an interface for external objects to get and set data. An instance variable is internal to that object, although it can be exposed to outside objects using properties, its own getter/setter methods, or indirectly through other public methods you create.
Marc Charbonneau
In other words, the property statement doesn't create something in the instance object. It just tells the outside world how to get something from the object. That something might be an instance variable, or if might be any piece of information that you want to write getter and setter method for. As for instance variables, they can be accessed just like you can in C and C++.
Vagrant
A: 

THANKS FOR THIS INFORMATION, BUT I REALLY NOT UNDERSTOOD WHY SHOULD I PUT NONATOMIC AND RETAIN IN IN THE PARENTHESIS.

@property(nonatomic, retain) IBLabel *label;

PLEASE HELP ME OUT.......

THANKS.

SUDIPTA
First off, you should be asking a new question, not hijacking an 11-month old question. You put `nonatomic` and `retain` in the parenthesis because that's how the syntax is defined. You also shouldn't write in all caps. It's annoying, and makes us think you're username is misspelled.
Dave DeLong
A: 

property is use the accessors methods behavior the method any in the class or section

upendar.nelluri