Differences between member variables and attributes in iOS and ios

Source: Internet
Author: User

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,@propertyBy default, a member variable of the _ type is generated for declared attributes.setter/getterMethod.
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.@synthesizeMethod.
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 variableThree steps.
If we only writeMember variable + @ property:

@interface GBViewController :UIViewController{    NSString *myString;}@property (nonatomic, strong) NSString *myString;@end
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
Warning will be reported during compilation: Autosynthesized property 'mystring' will use synthesized instance variable' _ mystring', not existing instance variable 'mystring'
  • 1
  • 2
  • 3
  • 1
  • 2
  • 3

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,@propertyDeclared attributes not only generate a _ type member variable by default, but also generatesetter/getterMethod.

In.mThe compiler automatically generates an instance variable._myString. You can directly use it in the. m file._myStringInstance variables, you can also use attributesself.myString. Are all the same.

Note thatself.myStringActually calledmyStringAttributesetter/getterMethod. 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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 1
  • 2
  • 3
  • 4
  • 5

In this Code, only a member variable is declared, and nosetter/getterMethod. Therefore, when accessing member variables, you can directly accessnameCan also be used like C ++self->nameBut 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 signOn the left,setterMethod. If the dot expression appears inOn the right sidegetterMethod.
  • OCPoint expression (.)Actually, it is called objectsetterAndgetterA shortcut of the method,Self. myString = @ "James ";Actually[Self setmyString: @ "James"];

First, we need to understand,@synthesizeGeneratedsetter/getterMethod.
Although it is used directly now@propertyThe compiler automatically generates instance variables starting with the following line._myStringYou do not need to manually write instance variables. And not in the. m file@synthesize myString;Generatesetter/getterMethod. 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 variableWhat about it?

The answer is no.
BecauseMember variable + @ property + @ synthesize member variableThe compiler will not help us generate_ Member variablesSo it will not operate_ Member variablesNow;
At the same time@synthesizeYou can also specify the instance variables corresponding to the attributes,
For example@synthesize myString = xxx; 
Soself.myStringIt is actually the instance variable xxx for the operation, rather than the _ String.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.