After a period of iOS development using the Objective-c language, it was found that its language base was relatively weak and began to compensate for its shortcomings. I found that after using a language, many of the principles behind it would find a deeper understanding. The following is the confusion of my properties and member variables of the use and relationship problems, because the level of limited may be wrong, please read the article to correct.
1. Properties
The use of attributes in Apple's official document "The OBJECTIVE-C programming Language" in the detailed description, here will not repeat, the link is as follows:
"The Objective-c programming Language"
If your English is not good, it does not matter, has been all translated this document, the link is as follows:
Objective-c Official document translation for programming languages
2, about @synthesize object = _object Explanation
We'll see this in a lot of code:
[Plain] view plaincopy
- @interface myclass:nsobject{
-
Myobjecct *_object;
-
}
-
@property (nonamtic, retain) Myobjecct *object;
-
@end
-
@implementatin MyClass
-
@synthesize Object=_object;
I looked up some of the blogs I wrote with other people on the internet and summed up several reasons for this:
(1) Differences between 32-bit systems and 64-bit systems
In a 32-bit system, if the @interface part of the class does not have a Ivar (instance variable) declaration, but there is @property declaration, there is a @implementation in the @synthesize part of the class, You will get a compilation error similar to the following:
Synthesize property ' XX ' must either is named the same as a compatible Ivar or must explicitly name an Ivar
On a 64-bit system, the runtime automatically adds Ivar to the class, and the added Ivar is prefixed with an underscore "_".
(2) Avoid puzzling bugs
Here's a brief look at the difference between _object and object. _object is a member variable of the MyClass class, and object is a property. Property and synthesize define a pair of getter and setter methods, where the Getter method is the Object,setter method is setobject, in fact the getter and setter methods manipulate the variable _object.
If you write synthesize objec = _object, The Getter method is:
[Plain] view plaincopy
-
-(MyObject *) object
-
{
-
return _object;
-
}
If you write synthesize object, the Getter method is:
[Plain] view plaincopy
-
-(MyObject *) object
-
{
-
return object;
-
}
Unexpected errors occur when the name of the function and the name of the attribute are the same, and in order to avoid this bug,apple, most of the demo code is used in this way.
(3) Use of attributes and variables
The Self.object property is invoked using the Getter method, which can be used outside the class. The variables are called through _object and can only be used in the corresponding implementation of the class and cannot be used outside the class.
Let's look at two assignment actions:
[Plain] view plaincopy
-
Self.object = [[MyObject alloc] init];
-
Object = [[MyObject alloc] init];
The first approach relates to @property (Nonamtic,retain), which is actually assigned by invoking the setter method SetObject. The second way is a simple pointer assignment, without invoking the setter method.
Here are the changes in Retaincount:
[Plain] view plaincopy
-
MyObject *tmp = [[MyObject alloc] init];
-
Self.object = tmp; Retaincount = 2
-
[TMP release]; Retaincount = 1
-
_object = [[MyObject alloc] init]; Retaincount = 1
Analysis on the usage and relationship of the attributes and member variables of OBJECTIVE-C