Property: Only the getter and setter methods have been declared, and nothing else has been done.
Synthesize: The getter and setter methods are implemented mainly, and automatically add you to the instance variable if there is no instance variable. The implementation is mainly based on the property characteristics of the settings, such as the property set to copy (retain), ReadOnly, Assgin and so on.
Dynamic: Basically tell the compiler to say don't give me the Generate instance variable, getter, setter method. I'm going to build it myself. If you do not generate it yourself when you invoke a getter or setter method, the program will take place crash.
On the Code
In the header file:
@interface person:nsobject{ @private nsstring *name; Nsinteger age;} @property (nonatomic, copy) NSString *name; @property (nonatomic, assign) Nsinteger age;
(1) In the implementation file:
@synthesize name; @synthesize age;
This is written in the implementation file. It's really perfect. This makes it natural to generate getter and setter methods for name, age instance variables.
(2) Change the wording in the real document
@synthesize name = _name; @synthesize age;
It is important to note here that @synthesize name = _name; This sentence is divided into three parts. Part I: @synthesize: The key word for OC. Part Two: Name: The name here refers to the identifier of the variable behind the property, if I @property the file (nonatomic, copy) NSString *name; the word "name" Changes to "nickname ", then the name in the @synthesize should also be changed to nickname, otherwise it will be wrong. Part III: _name, maybe you will be confused here, why come out a _name logo? And it hasn't gone wrong. The first thing we want to make clear is that the third part refers to the instance variable. It also says that the instance variable should correspond to the property. You may find that there is no _name instance variable in my header file. Wouldn't that be an error? The answer is no, because @synthsize has the ability to automatically create instance variables. If the compiler does not see _name in the instance variable list, then the compiler automatically creates an _name instance variable. So we can call the power variable somewhere else: _name = @ "Hello"; that's right.
(3) Change the implementation file
@dynamic name;
If this is written. This means that the getter method and setter method for name are not implemented. If you call self setName in your program: This method will error.
Summarize:
In fact, @property just illustrates how we declare a method. And @synthesize tells us who the protagonist is in the method, and says to assign a value to that instance variable.
Attention:
Now we write the program may only need to write @property (nonatomic, retain) NSString *nickname; Did not find anything @synthesize, then is because now a sentence @property quite do two things, one thing his original meaning declares getter and setter method. One is: @synthesize nickname= _nickname; So we are now instance variables are underlined ah "_". This is a good programming specification.
The differences and differences between property synthesize dynamic