@property @synthesize

What do @synthesize and @property do in Xcode? Please provide an explanation in really simple terms?


You asked for simple terms:

@property declares a property in your class header

@property (nonatomic, retain) NSString *myString;

@synthesize creates your setter and getter for your property (accessor methods)

Without synthesize you have to write your own setter and getter implemention, like getMyString or setMyString (capitalize the first character of your property)

Sam: Just an advice: http://www.cocoadevcentral.com/d/learn_objectivec/ is a pretty solid resource to learn about basics like properties.

Good Luck!


Properties and synthesized accessors are new features in Objective-C 2.0.

When you declare a @property you declare somewhat an instance var. Then you @synthesize accessor methods (i.e. getter and setter) for that property.

There are also @dynamic accessors if you're interested.

You should really do your homework on this. Apple has nifty pdf for that.


Think of all objective-c magic as just a "smarter macro", like a "smarter #define statement" @property if you notice is always in the h file, @synthesize is always in the m file. So in the background @property (whatever) NSString *myString;

becomes a declaration of 2 methods and a private variable;

void set_myString:(NSString *) str;
(NSString*) get_myString;

declarations in the header file

to make them do something their implementation is added into m file when you type in @synthesize myString; which becomes something like void set_myString:(NSString *)str { myString = str; }

(NSString *) get_myString
{
  return (myString);
}

But it's smarter than this depending on if you say "retain" "strong" or "weak" it will either just return the pointer to the myString or it will copy the myString into a new object

So all of this is done automatically by a compiler just by reading your declarations. Which is quite useful and saves a lot of time