After using objective-C language for iOS development for a period of time, I found my language base was relatively weak and began to make up for my shortcomings. I found that after using a language, I can look back at many of its principles to find a deeper understanding. The following describes the usage and relationship of attributes and member variables that have been confusing to me. Due to the limited level, errors may occur.
1. Attributes
The usage of attributes is described in detail in Apple's official document "the objective-C programming language". The link is as follows:
The objective-C Programming Language
If your English is not good, it doesn't matter. Someone has translated all the documents. The link is as follows:
Official objective-C Programming Language Documentation Translation
2. Explanation of @ synthesize object = _ object
We will see this in many codes:
@interface MyClass:NSObject{ MyObjecct *_object;}@property(nonamtic, retain) MyObjecct *object;@end@implementatin MyClass@synthesize object=_object;
I read some documents and other blog posts on the Internet, and summarized the reasons for writing these articles as follows:
(1) differences between 32-bit and 64-bit Systems
In a 32-bit system, if the @ Interface part of the class is not declared as Ivar (instance variable), but there is a @ property declaration, @ synthesize is returned in the @ implementation part of the class, the following compilation error is returned:
Synthesize property 'xx' must either be named the same as a compatible Ivar or must explicitly name an Ivar
In a 64-bit system, the system automatically adds Ivar to the class during runtime. The added Ivar is prefixed with an underscore.
(2) Avoid inexplicable bugs
The difference between _ object and object is briefly described here. _ Object is a member variable of the myclass class, and object is an attribute. Property and synthesize define a pair of getter and setter methods. Here, the getter method is object and the setter method is setobject. In fact, the getter and setter Methods Operate on the Variable _ object.
If you write synthesize objec = _ object, the getter method is:
-(MyObject *)object{ return _object;}
If the getter method is:
-(MyObject *)object{ return object;}
When the function name and attribute name are duplicated, unexpected errors may occur. To avoid this bug, most of the demo codes provided by Apple also adopt this method.
(3) usage of attributes and variables
The property is called by using the self. Object method and can be used outside the class. Variables are called through _ object and can only be used in implementation corresponding to the class, but not outside the class.
Let's take a look at the two assignment operations:
self.object = [[MyObject alloc] init];_object = [[MyObject alloc] init];
The first method is related to @ property (nonamtic, retain). In fact, values are assigned by calling the setter method setobject. The second method is simple pointer assignment without calling the setter method.
The changes in retaincount are as follows:
MyObject *tmp = [[MyObject alloc] init];self.object = tmp; //retainCount = 2[tmp release]; //retainCount = 1_object = [[MyObject alloc] init]; //retainCount = 1