codecowboy
codecowboy

Reputation: 10095

Objective-C Properties in iPhone Development

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.

Upvotes: 8

Views: 5449

Answers (3)

Sanoj Kashyap
Sanoj Kashyap

Reputation: 5060

The difference between Property and Instance ivar is, the variable which make as Property that can be visible in another Class whereas for accessing the iVar or instance you need to create the Object of that class and then you can access. and With use of @synthesize compiler will generate the setter and getter for that property.

-(TYPE)name;-getter Method

-(void)setName:(TYPE)aName; setter Method

Upvotes: 0

Peter N Lewis
Peter N Lewis

Reputation: 17811

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.

Upvotes: 7

Kevin
Kevin

Reputation: 2802

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"

Upvotes: 4

Related Questions