tek3
tek3

Reputation: 2115

What exactly is a property in Objective C ? What is the difference between a property and an instance variable?

I am very much confused between instance variables and property. I have read number of posts regarding this but still i am not clear about it. I am from JAVA background and what i infer from objective C documentation is that a property is similar to JAVA BEAN CLASS (one having getter and setter of instance varibles). A property can accessed from other classes through its getter and setter methods while an instance variable is private and cannot be accessed from other classes.

Am i right in thinking in this direction ?

Upvotes: 3

Views: 1134

Answers (7)

RichWalt
RichWalt

Reputation: 522

I know this subject has been beat to death here ... but some seem to be focusing on the technical details, whereas I wanted mention something along the lines of the BIG PICTURE ...

Think of properties as kind of first-class ivars. Both properties and ivars may model attributes of an object ... but an ivar gets special attention if you go ahead and set it up as a property. Basically, you should an attribute as a property (as opposed to an ivar) if you feel it needs getter / setter methods. Dot notation makes for very readable code. This may help in deciding when to declare a variable as a property as opposed to simply using a regular ivar.

Upvotes: 2

tek3
tek3

Reputation: 2115

I found this amazing thread which clearly explains each and evrything about properties.

http://www.iphonedevsdk.com/forum/iphone-sdk-tutorials/7295-getters-setters-properties-newbie.html

Thank you all for your responses.

Upvotes: 0

Enrique
Enrique

Reputation: 126

Well, maybe it was not clear that a property does not need an instance variable. You can define a read-only property based on any calculation on instance variables or any other variables in the scope. The issue here is that you must manually code the getter.

@interface person:NSObject{
    NSDate *birthDate;
}
@property(readonly) int age;
@end

@implementation

-(int) age{
 // return calculated age based on birthDate and today;
}
@end

The name of the property does not need to be the same as the instance variable.

@synthesize myProperty = myVar;

Upvotes: 1

iOSPawan
iOSPawan

Reputation: 2894

Ok, instance variable and property is far away from each other. instance variable is a state of object and property is a assecor method(getter/setter) of that state(instance variable).

So whenever you create an property in header file. compiler convert those property in to accessor method. suppose you declared property - @property(nonatomic, assign, readwrite) NSString *name; So compiler will be converted those in to

-(NSString *)name;
-(void)setName:(NSString *)name;

And then for definition for accessor method there is two way.

  1. manually - use dynamic in implementation file(.m) and then give the definition of accessor method by doing this you won't get any warning.

  2. Let compiler do the job - this can be done by synthesizing property e.g synthesize name;. so now compiler will generate the definition for the accessor method for you.

Hope it helps ;)

Upvotes: 2

He Shiming
He Shiming

Reputation: 5819

The primary difference between instance variable and property is that for properties, the compiler will automatically generate a getter/setter method pair. For instance:

@property (nonatomic) int value;

will generate:

-(void)setValue:(int)i
{
    value = i;
}
-(int)value
{
    return self->value;
}

given @synthesized.

If you crab a book on Objective-C 1.0, you'll notice that this feature isn't available. This is a new feature in 2.0, also known as the dotted syntax. It's introduced mainly because the complicated getter/setter syntax.

The benefit of this feature is that even though you have the compiler automatically declared the pair for you, you can still manage to override it. For instance, you can still have -(void)setValue:(int)i declared as a method of your class, and override the behavior. This is useful in scenarios of validation, such as you want to put a limit on the range of value.

As far as Java is concerned, Objective-C actually do have @public instance variable syntax, but it's a habit not to use it. It's sort of similar to Java's concept of protecting a private variable through getter/setter. But its primary objective-c is to override getter/setter and minimize syntax.

Now this is just a preview, refer to http://cocoacast.com/?q=node/103 or some objective-c 2.0 books if you wanted to know more.

Upvotes: 1

Jorge Aguirre
Jorge Aguirre

Reputation: 2857

A property in objective c is in fact the setter and getter methods that make it possible to access an attribute in a class from outside of it. So when you declare for example

@interface example:NSObject{
    NSString *variable;
}
@property(readwrite, assign) NSString *variable;
@end

@implementation
@synthesize variable;
@end

You are in fact declaring the methods

-(NSString *)getVariable;
-(void)setVariable(NSString *)value;

And you can access then by using the point notation and the name of the property, like

instance.variable = something;
something = instance.variable;

Upvotes: 1

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726479

The parallel with Java is very good. The only difference is that Objective C provides a way to access a property as if it were a variable, and Java does not. The other difference is that in Objective C you can synthesize properties, while in Java you need to write your getters and setters manually.

Property is a "syntactic sugar" over a getter method or a pair of a getter and a setter methods. Properties are often (but not always) backed by an instance variable, but they can be implemented in any way that you can implement a parameterless instance method.

Upvotes: 4

Related Questions