简体   繁体   中英

@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 (ie 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

it simply sets the property's setter variable name in it's own class. For example lets say I have this: @property (nonatomic, copy) NSArray* viewControllers;

Well if i want to access the setter _viewController i wouldn't set a synthesize variable. but if i want to access the viewController variable by the name viewController instead of _viewController , I would do @synthesize viewController; .

If I wanted to use it as a completely different name I could do this @synthesize viewControllers = viewControlololer; but that's only setter. As you will notice [self viewControllers] only works and not [self viewControlololer];

So I don't understant why everyone writes sets the "setter and getter" of a property. It doesn't change the getter variable at all... unless that means [self viewController] is aware of viewControlololer (obviously).

Actually properties are synthesized, either implicitly or explicitly. Properties are implicitly synthesized. So there's no need to use synthesized unless you wanna change the name of the variable to something different than _property_name.

There are other use cases, for example if you don't want an instance variable to back your property.

Properties are explicitly synthesized by using the @synthesized directive.

(Answer extracted from the Big Nerd Ranch guide)

By default all our variables are Private so we can't acess out of the class. if we want to use our instance variable in out of the class. When you declare a @property you declare somewhat an instance var. Then you @synthesize accessor methods (ie getter and setter) for that property.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM