The evolution of setter and getter, immediately following setter and Getter Beginner
[email protected] and @synthesize
These two keywords appear to eliminate setter methods and getter methods in the code
@property: You can automatically generate setter and getter declarations for a member variable
@property int age;
The equivalent of the following two sentences:
-(void) Setage: (int) age;
-(int) age;
@synthesize automatically generates the setter and getter implementations of age and accesses the _age member variable
@synthesize age = _age;
In this way, we omit the time to write setter methods and getter methods, but it is also extremely boring and meaningless to write.
but even so, it's very uncomfortable, in the old version of Xcode, to avoid writing setter methods and Getter methods, We also have to write @property int _age between the @interface and the @end; Write @synthesize code between @implementation and @end, and fortunately, you are living in a happy time, since xcode4.4, @property int _age; This code put @property and @synthesize work to dry.
It mainly does the following things:
1) Generate a declaration of the Get and set methods of the _age member variable;
2) generate the _age member variable set and get method implementation;
3) generate a _age member variable.
Of course, the getter method and setter method generated by the @property method may not meet your needs, you can also manually add getter methods and setter methods, if manually written setter method @property will not generate setter method, However, the getter method is generated, and the getter is the same, if you manually write the setter method, the Getter method, @property will not generate the appropriate getter method and setter method for you.
Example:
Teacher.h
@interface Teacher:nsobject
@property NSString *name;
@property NSString *gender;
@end
Teacher.m
@implementation Teacher
@synthesize name = _name;
@synthesize gender = _gender;
@end
[Email protected]
Example:
@property (nonatomic,copy) NSString *name;
@property (Nonatomic,retain) NSString *gender;
@property (nonatomic) Nsinteger age;
/*
Parameters of the property:
Atomicity: Atomic to attribute lock, ensure multithreading thread security, default value
Noatomic property is not locked, multithreaded under unsafe, but fast, usually use!
Read-write properties: ReadWrite generate getter and setter methods, default values
ReadOnly only generate Getter methods, read-only
Set method Processing: Assign direct assignment, default value
Scope of Use:
Theoretically: All data types
In fact: basic data types
Assign (basic data type) mode:
-(void) Setage: (Nstnteger) age{
_age = age;
}
Retain in the Set method, the memory optimization, first release the original value, and then retain the new value
Scope of Use: Data for all object types is supported. Includes: System-provided classes and custom classes
Retain Way:
-(void) Setdog: (Dog *) dog{
if (_dog! = dog) {
[_dog release]; First release the original value
_dog = [dog retain]; Re-retain new value
}
Copy in the Set method, for memory optimization, first release the original value, and then copy the new value
Scope of use: General NSString and Block
All objects adhering to the Nscopying agreement are supported <NSCopying>
*/
Special Note: The above setter is written in the MRC environment, in the ARC environment, there is no retain and release (because the ARC environment is automatically managed reference count), retain is a purely reference count plus 1:
Memory management mechanism: reference counting mechanism
MRC: Manually Manage reference counts
ARC: Automatic management of reference counts
Setter and Getter advanced and memory management beginner