Differences between member variables and attributes in iOS and ios
Link: http://blog.csdn.net/u012946824/article/details/51788565
History:
People who contact iOS know that,@property
By default, a member variable of the _ type is generated for declared attributes.setter/getter
Method.
However, this is only a new mechanism launched by Apple after iOS5. When looking at the old code, we often see that a member variable is defined in braces and @ property declaration is used, and it is also used in @ implementation.@synthesize
Method.
As follows:
Demo
@ Interface ViewController () {// 1. declare the member variable NSString * myString;} // 2. use @ property (nonatomic, copy) NSString * myString; @ end @ implementation ViewController // 3. finally, use synthesize in @ implementation to generate the set Method @ synthesize myString; @ end
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
In fact, the root cause of this situation is that Apple converts the default compiler from GCC to LLVM (low level virtual machine
.
Before changing the attributesMember variable + @ property + @ synthesize member variable
Three steps.
If we only writeMember variable + @ property
:
@interface GBViewController :UIViewController{ NSString *myString;}@property (nonatomic, strong) NSString *myString;@end
Warning will be reported during compilation: Autosynthesized property 'mystring' will use synthesized instance variable' _ mystring', not existing instance variable 'mystring'
But after being replaced with LLVM, the compiler will generate an instance variable starting with an underscore when no new instance variable is found during compilation. So now we don't have to declare an instance variable. (Note:
: = Is not necessary, not =)
Of course we are also familiar,@property
Declared attributes not only generate a _ type member variable by default, but also generatesetter/getter
Method.
In.m
The compiler automatically generates an instance variable._myString
. You can directly use it in the. m file._myString
Instance variables, you can also use attributesself.myString
. Are all the same.
Note thatself.myString
Actually calledmyString
Attributesetter/getter
Method. This is different from the use of the dot in C ++. The dot in C ++ can directly access the member variable (that is, the instance variable ).
For example, the following code is available in oc:
@interface MyViewController :UIViewController{ NSString *name;}@end
In this Code, only a member variable is declared, and nosetter/getter
Method. Therefore, when accessing member variables, you can directly accessname
Can also be used like C ++self->name
But it cannot be used.self.name
.
- Extension: Many people think that the point syntax in OC is strange. It is actually intended by the OC designer.
Point expression (.)
It looks a little similar to the structure access in the C language and the object access in the java language, if the dot expression appears in the equal sign=
On the left,setter
Method. If the dot expression appears in=
On the right sidegetter
Method.
- OC
Point expression (.)
Actually, it is called objectsetter
Andgetter
A shortcut of the method,Self. myString = @ "James ";
Actually[Self setmyString: @ "James"];
First, we need to understand,@synthesize
Generatedsetter/getter
Method.
Although it is used directly now@property
The compiler automatically generates instance variables starting with the following line._myString
You do not need to manually write instance variables. And not in the. m file@synthesize myString;
Generatesetter/getter
Method. But when looking at the old code, we can still see that someone is usingMember variable + @ synthesize member variable
.
The problem arises:
Can we consider that the new compiler under LLVM@property
= The GCCMember variable + @ property + @ synthesize member variable
What about it?
The answer is no.
BecauseMember variable + @ property + @ synthesize member variable
The compiler will not help us generate_ Member variables
So it will not operate_ Member variables
Now;
At the same time@synthesize
You can also specify the instance variables corresponding to the attributes,
For example@synthesize myString = xxx;
Soself.myString
It is actually the instance variable xxx for the operation, rather than the _ String.