Hello,
I've read many books about Iphone dev. I'm new to objective c.
Why we need to add property and synthesize?
Can someone explain and relate it to real life story.
eg: pointer - we call cop to catch criminal
Thank you
Hello,
I've read many books about Iphone dev. I'm new to objective c.
Why we need to add property and synthesize?
Can someone explain and relate it to real life story.
eg: pointer - we call cop to catch criminal
Thank you
I think you're new to OOP, too. This is the reason why you don't understand the use of properties. I've not read any iphone development book and i don't know if they talk about OOP, but I suggest you to focus on this subject before.
Anyway you use properties to decouple data from access and manipulation of data.
Instance variables in Objective-C are by default private (like in java: private int) so, you need accessors (getters/setters) to get and set the instance variable. So, @property tells the compiler to automatically generate those getters/setters for your instance variable, you can specify some options to it like:
@property (readonly) UILabel *labelName;
Which will make the compiler generate just a getter without a setter for this property. So, basically the @property keyword tells the compiler to generate getter/setter based on options you provide.
The @synthesize keyword will complete the implementation of @property and generate the getter/setter for you. This is required because anything written in the header (.h) file has to be reflected on the implementation file (.m). So, @property for the .h and @synthesize for the .m.
Here is an example (.h file)
@interface QuickStartViewController : UIViewController
{
NSString* name;
}
@property (retain) NSString *name;
(.m) file
@implementation QuickStartViewController
@synthesize name;
I recommend to take a look to this apple docs: This is the old and classic way, properties just simplifies things but if you are new to Obj-C it can be confusing. They are just not accessors, the important part are the "copy", "retain", "assign", "readonly", etc http://developer.apple.com/mac/library/documentation/Cocoa/Conceptual/ObjectiveC/Articles/ocProperties.html
Another reason that is not written here is that: when you get serious, you can implement Key-Value Observing, Key-Value coding, etc, like in CoreAnimation, Notifications, etc.
And I admit it to, I just take a look to your profile, lol.
The @property keyword and its related friends @synthesize and @dynamic provide a short hand method for declaring and defining accessor methods for object properties.
As an example, the following:
//interface
@property (retain) id foo;
//implementation
@synthesize foo = fooIvar;
is more or less equivalent to
//interface
-(id) foo;
-(void) setFoo: (id) newFoo;
//implementation
-(id) foo
{
id ret;
@synchronized(self)
{
ret = [[fooIvar retain] autorelease]; // don't return from inside the sync block to avoid spurious warning
}
return ret;
}
-(void) setFoo: (id) newFoo
{
@synchronized(self)
{
[newFoo retain];
[fooIVar release];
fooIVar = newFoo;
}
}
Note that the above assumes automatic KVO notifications.
In the old days, before properties, you would have found yourself writing stuff like the above quite a lot.