views:

78

answers:

4

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

A: 

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.

mp
I've learned c, java and oo Php(codeigniter).but these extra syntax makes me crazy.currently ready "iPhone and iPad Apps for Absolute Beginners". thank you.
neotorama
Properties let you write less code. You don't have to write long lists of get and set methods. The compiler will write them for you.Maybe apple documentation can help you. There are several articles for beginners too.
mp
A: 

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;
Salem Sayed
Actually by default they are protect Salem.
nacho4d
sorry, is @protected .The keyword @property does not automatically generate accessors (getters/setters) it just declares it. Keyword @synthesized does that job, but that is optional, you could write your accessors by your own.
nacho4d
A: 

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.

nacho4d
"They are just not accessors". Yes they are. "Another reason that is not written here is that: when you get serious, you can implement Key-Value Observing, Key-Value coding, etc". You don't need to use properties to access all of those things.
JeremyP
I should said they are just NOT SIMPLE accessor, because they can be used for many other things. And YES, we need properties to access those things: For example [CABasicAnimation animationWithKeyPath:@"position"]; we can write the string @"position" since is a property of CALayer. The same in KVO, ie.:[account addObserver:inspector forKeyPath:@"openingBalance" options:NSKeyValueObservingOptionNew context:NULL];you can write @"openingBalance" because is a property of account object, you cannot write just the name of the member variable. Otherwise you will be observing/modifying nothing.
nacho4d
Above code link: http://developer.apple.com/mac/library/documentation/Cocoa/Conceptual/KeyValueCoding/Concepts/BasicPrinciples.html
nacho4d
+2  A: 

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.

JeremyP