1: What is @property?
In Objective-c, @property is the syntax for declaring attributes, which makes it quick and easy to create getter/setter methods for instance variables.
2: The nature of @property?
@property = Ivar + setter + getter, is actually the instance variable + getter Method + Setter method.
3: The role of @property?
@property (attributes) as an attribute of objective-c, the main function is to encapsulate the data in the object. The instance variables in Objective-c are usually accessed through the Getter/setter method. For an instance variable of a given name in an object, the compiler automatically generates the Getter/setter method.
@property is usually used in the work as follows:
@interface**cartype; @end
The above code is equivalent to the following code:
@interface car:nsobject{ // Declaration of two instance variables nsstring *carname; *Cartype;} -(void) Setcarname: (NSString *) carname; -(NSString *) carname; -(void) Setcartype: (NSString *) Cartype; -(NSString *) Cartype; @end
Visible: Declares an instance variable and declares the access method that implements the variable, and the effect is the same as @property.
4: How is the @property,ivar, getter, setter generated and added to the class?
After you declare a property with @property, the compiler automatically generates an instance variable, and an access method for that instance variable. This process is called automatic synthesis (autosynthesis). The process is completed at compile time, so the source code of the access method is not visible in the compiler.
5: What is the name of the @property generated instance variable?
If you do not use @synthesize to specify the name of the instance variable, the name of the instance variable that is generated by default using @property is the underscore + property name. For example, in the code above:
@property (nonatomic, copy) NSString **cartype;
The instance variable names generated during compilation are _carname, _cartype, and _carname, _cartype can be used directly in the program.
You can use the @synthesize to specify the name of the instance variable, as follows:
@synthesize carname = mycarname; @synthesize cartype = Mycartype;
In this way, the two instance variable names generated in the program are Mycarname, Mycartype, and can be used directly.
If @synthesize is used, but the instance variable name is not specified, as follows:
@synthesize Carname,cartype;
In this case, the two instance variable names generated in the program are Carname, Cartype, and can be used directly.
The @property in Objective-c