Dowemo

  • Changes to properties
  • Difference between property and synthesize
  • Just touch ios, and later learn the code, find the definition attributes sometimes write between the m file @ inferface and end or sometimes involved in @ synthesize. It was later found that as the version of ios continues to grow, the attributes are somewhat different. The declaration of the current property:

@property (nonatomic,strong) NSString *myTitle;





As we declare an attribute, the compiler automatically generates the corresponding instance variable mytitle for us, and, of course, we can manually generate the corresponding instance variable mytitle in the m file via the keyword synthesize. You can do this without adding an underscore. First we know that the attribute is a new language mechanism for oc, and the declared attribute must have a corresponding instance variable A change to the property: Before apple uses the fink compiler, the use of properties is: Manually declare the instance variables, as follows: H file:

@interface ViewController: UIViewController


{


//属性的实例变量 


NSString *mytitle;


}


@property(copy,nonatomic) NSString *myTitile;


@end





Corresponding. M file.

//编译器遇到@synthesize会自动实现setter和getter方法


//编译器遇到@synthesiz会去访问同名的myTitle,如果没有找到就会报错。


@synthesize myTitle





While apple started using the xfs compiler, the compiler could automatically generate an instance variable with an underscore name for the property Property change steps First edition:

.h文件:


@interface ViewController: UIViewController


{


//属性的实例变量 


NSString *mytitle;


}


@property(copy,nonatomic) NSString *myTitile;


@end





Corresponding. M file.

//编译器遇到@synthesize会自动实现setter和getter方法


//编译器遇到@synthesiz会去访问同名的myTitle,如果没有找到就会报错。


@synthesize myTitle





Second edition Instead of having to declare a member variable manually, the keyword @ synthesize will default to access the mytitle with the same name, if it isn't found H file

@property (nonatomic,strong) NSString *myTitle;





M file

//编译器遇到@synthesize会自动实现setter和getter方法,访问同名变量myTitle。如果找不到的话会自动创建一个私有同名变量myTitle,因此之后可以不用手动声明一个实例变量


@synthesize myTitle;





Third edition Co & ider the method name of the variable name and getter, the argument name of the setter, causing a warning, so the default give the attribute an underscore M file

//指定_myTitle作为myTitle的实例变量


@synthesize myTitle = _myTitle;





As the ios upgrade, @ synthesize can also be omitted, simply declaring attributes in the h file.

@property (nonatomic,strong) NSString * myTitle;





Summary: the compiler automatically generates instance variables when the attribute is declared, the nature of the instance variable is the member variable, the Two, property and synthesize differences Effect of synthesize: 1, enabling the compiler to automatically generate setter and getter methods 2, specifying the I & tance variable that corresponds to the property: If @ synthesize xxx is written in the m file, then the generated instance variable is xxx; it's if it isn't written. In previous versions, @ property can only be written in the h file, and @ synthesize can only be written in the m file. And now @ is property @ property and @ synthesize. For @ property ( nonatomic, strong ) nsstring * xxx, 3 features are implemented: 1 ) the get and set methods of the str member variable are generated 2 ) the implementation of the get member variable set and method 3 ) generate a member variable of str ( private ) Summary: the compiler automatically generates instance variables when the attribute is declared, the nature of the instance variable is the member variable, the



Copyright © 2011 Dowemo All rights reserved.    Creative Commons   AboutUs