Detailed description of attributes and error points of OC learning notes, and oc learning notes
The attribute concept exists in OC1.0. The format is to define instance variables, and then define the setter and getter methods,Operate attributes with vertex Operators
For example, the class interface Section
1 @ interface Father: NSObject 2 {3 NSInteger _ item; 4} 5 # pragma mark-attribute 6-(void) setItem :( NSInteger) item; 7-(NSInteger) item; 8 9-(void) setAAA :( NSInteger) aaa; 10-(NSInteger) AAA; 11 12 @ end
Class implementation
1 #import "Father.h" 2 3 @implementation Father 4 5 -(void)setItem:(NSInteger)item{ 6 _item = item; 7 } 8 -(NSInteger)item{ 9 return _item;10 }11 12 -(void)setAAA:(NSInteger)aaa{13 _item = aaa;14 }15 -(NSInteger)AAA{16 return _item;17 }18 19 @end
Main Function
1 Father *test = [[Father alloc]init];2 test.item = 19;3 NSLog(@"%ld",test.item);4 5 test.AAA = 20;6 NSLog(@"%ld",test.AAA);
In the preceding example, an instance variable and two attributes are defined, and the two attributes operate on the same instance variable. Note that attributes and instance variables are not necessarily related, attributes are only instance variables (or even instance variables are not operated, which will be explained in detail later). However, the naming rules for setter and getter methods must be noted that the setter method is named set + attribute name, the getter naming method is the property name (the setter method of the property AAA is setAAA:, and the getter method name is AAA)
After OC2.0, the @ property and @ synthesize keywords are provided to simplify attribute writing from the syntax layer.
Attribute Declaration
1 # import <Foundation/Foundation. h> 2 3 @ interface Father: NSObject 4 {5 NSInteger _ item; 6 NSString * _ str; 7} 8 # pragma mark-attribute 9 @ property NSInteger item; 10 @ property NSString * AAA; 11 12 @ endView Code
Attribute implementation
1 @ implementation father3 @ synthesize item = _ item, AAA = _ str; 4 5 @ endView Code
Note that two different attributes cannot operate on the same instance variable at the same time,An error is reported during the syntax check..
After Xcode5, the code for Attribute implementation can be omitted. Xcode will generate the code for Attribute implementation (officially recommended by Apple)
Here, you need to pay attention to a lot of points for one-to-one analysis.
After omitting the attribute implementation part of the code, Xcode provides the default attribute Implementation Code as follows:
@ Synthesize attribute 1 = _ attribute 1, attribute 2 = _ attribute 2;
That is, when the instance variable of the attribute operation is _ + attribute name, the above attribute declaration code is used as an example. The code generated by Xcode is as follows:
@ Synthesize item = _ item, AAA = _ AAA;
Note that only _ item and _ AAA in the class interface file do not exist. This variable does not exist. It is generated automatically by Xcode. private instance variable, why is it true? Because this variable will not be generated in the interface file, it will only be declared in the class implementation file, and it is declared at the top of the class, this is probably the case.
1 @ implementation father3 NSString * _ AAA; 4 5 @ synthesize item = _ item, AAA = _ AAA; 6 7 @ endView Code
Therefore, when Father is used as the parent class, _ AAA will not be inherited like the instance variables under @ private. Therefore, we do not recommend that you declare attributes with the same name as the parent class in the subclass, especially when there are no corresponding instance variables, you can consider the reasons.
Finally, we will briefly introduce the attribute modifiers in three categories.
Readwrite modifier: readwrite (default) readonly
Atomicity is also called thread security modifier: atomic (default) nonatomic
Semantic modifier: assign retain copy
The read/write modifier can control the read/write of attributes just like the word meaning. By default, it is readwrite readable and writable. When you set it to readonly, this attribute cannot be written, if you write a value assignment statement to an attribute, an error will be reported during compilation.
Thread security means to control whether the thread is secure (nonsense ...)
In this open a portal interested in children's shoes can look at the http://www.cnblogs.com/zwq194/archive/2012/06/26/2563567.html
Semantic modifiers are actually memory control.
Same portal http://blog.csdn.net/hahahacff/article/details/39839571