When setter, Getter,
In oC, the method for assigning Zhi to the instance variable is called setter)
The method used to read the instance variable value is called getter)
The value assignment methods we wrote earlier can be called setter and getter.
Setter and getter writing formats
The writing format of setter and getter in oC
If an instance variable is int age; or int_age;
Setter's writing format is as follows-(void) setage :( INT) age; that is, the instance variable name with the upper Letter of the set + initial letter (ignore the underline)
The getter writing format is as follows:-(INT) age; that is, the return value type is the same as the variable type, and the method name is the same as the instance variable name (ignore the underline)
Relationship between setter and getter and instance variables
Attribute Definition
Attribute Declaration: Use @ property to declare attributes (for example, @ property nsstring * Name ;)
Equivalent to two methods in @ Interface
-(Void) setname :( nsstring *) Name;
-(Nsstring *) Name;
Attribute implementation
Attribute implementation: Use @ synthesize to implement the attribute (for example, @ synthesize name = _ name)
Equivalent to the implementation of @ implementtation
-(Void) setname :( nsstring *) Name;
-(Nsstring *) Name;
Attribute attributes
Objective-C provides attributes to simplify programmer coding.
Some keywords are provided for attributes to control the implementation details of setter and getter.
These keywords are called attributes)
There are three categories of attributes.
There are three types of attributes.
Category 1
1. Read/write control (readonly, readwrite, setter, Getter)
If the keyword of the read-write control is readonly, it tells the compiler that only the getter method is declared (no setter method)
For example, @ property (readonly) nsstring * Name; equivalent to-(nsstring *) Name;
Example: @ property (readwrite) nsstring * Name;
It is equivalent to-(nsstring *) Name;
-(Void) setname :( nsstring *) Name;
Category 2
2. Atomic control (nonatomic, atomic)
If the keyword of atomic control is atomic. The setter and getter methods are absolutely secure under multi-thread access, that is, the setter and getter methods implement multi-thread access processing internally, and the default setting of atomic control is atomic;
If the atomic control keyword is nonatomic, the setter and getter methods do not implement multi-threaded access processing internally. They are just common multi-setter and getter methods.
..
In the process of program development, setter and getter are used everywhere. If atomic is used, it is necessary to constantly lock and unlock setter and getter to ensure thread access security and occupy system resources, reduces system performance.
It is set to nonatomic in the same city. Some attributes are defined as atomic only when thread security is required.
Example: @ property (readwrite, nonatomic) nsstring * Name;
Equivalent to;-(nsstring *) Name;
-(Void) setname :( nsstring *) Name;
Category 3
3. semantic settings (assign, retain, copy)
If the semantics is set to assign. The internal implementation of setter and getter is direct assignment,
Example: @ property (nonatomic, assign) nsstring * Name;
-(Void) setname :( nsstring *) Name
{
_ Name = Name;
}
-(Nsstring *) Name
{
Return _ name;
}
Example: @ property (nonatomic, retain) nsstring * Name;
-(Void) setname :( nsstring *) Name
{If (_ name! = Name ){
[_ Name release];
_ Name = [name retain];
}
}
-(Nsstring *) name {
Return [[_ name retain] autorelease];
}
For example, @ porperty (nonatomic, copy) nsstring * Name;
-(Void) setname :( nsstring *) Name
{
If (_ name! = Name ){
[_ Name release];
_ Name = [name retain];
}
}
-(Nsstring *) Name
{
Return [[_ name retain] autorelease];
}
Note that
If attributes are non-object type (such as int and float) attributes and Other semantic settings, only use assign.
If the attribute is of the object type (such as nsstring and nsarray ),
If the property is of the object type and you want to get the copy parameter, use the copy keyword.
-Usage of point syntax
For example: [person setname: @ "zhangsan"];
It can be equivalent to "person. Name = @" zhangsan ";
Nsstring * name = [person name];
It can be equivalent to nsstring * name = person. Name;
This article from the "Liu _ blog" blog, please be sure to keep this source http://liuyafang.blog.51cto.com/8837978/1543715
Objective-C setter, the relationship between getter and instance variables, attributes, and point syntax