In Objective-C, attributes combine new pre-compiled commands and new attribute access syntax. The new attribute function significantly reduces the number of lengthy code that must be written. Next we will compare the following code to copy the code // the first declaration method-(void) setRainHandling :( float) rainHanding;-(float) rainHandling;-(void) setSnowHandling :( float) snowHandling;-(float) snowHandling; // The second declaration method @ property float rainHandling; @ property float snowHandling; copying the above two pieces of code has the same effect, from this we can conclude that the role of the attribute is: @ property pre-compilation instructions are used to automatically declare the setter and getter methods of the attribute. In fact, the attribute name does not have to be the same as the instance variable name, but in most cases they are the same. We know the declaration of the settter and getter methods of the function, but how can we implement it? Let's take a look at the copy code // The first implementation method-(void) setRainHandling :( float) rh {rainHandling = rh;}-(float) rainHandling {return rainHandling;}-(void) setSnowHandling :( float) sh {snowHandling = sh;}-(float) snowHandling {return snowHandling;} // method 2 @ synthesize rainHandling; @ synthesize snowHandling; the purpose of copying the code is to completely delete the setter and getter methods, which can be completely replaced by the above two lines of code. The synthesize compiler has the following features: @ sythesize, which indicates "access code for this attribute is created ". When you run the @ sythesize rainHandling; line of code, the compiler adds the pre-compiled code that implements the-setRainHandling: And-rainHandling methods. Cocoa accessors can compile utility tools and UI generators on other platforms to generate source code, which will be compiled later. However, the @ sythesize pre-compilation command is different from the Code Generation Command. You will never see the Code implementing-setRainHandling: And-ranHandling. These methods do exist and can be called. In Versions later than X-code 4.5, you do not need to use @ sythesize. All attributes are variable-based. Therefore, when you synthesize the setter and getter methods, the compiler automatically creates instance variables with the same name as the attributes. If you do not declare these variables, the compiler will also declare them. If we do not implement @ sythesize ourselves, the compiler will generate an instance variable starting. For example, in the header file definition, write @ property (copy) NSString name; in its implementation, there will be an instance variable of _ name. Point expressions use the attributes of Objective-C 2.0 to reference some new syntax features, making it easier for us to access object attributes, that is, point expressions. If the dot expression appears on the left side of the equal sign (=), the setter method of the variable name will be called. If the dot expression appears on the right of the object variable, the getter method of the variable will be called. The dot expression is only a convenient way to call the access method, and there is no mystery. Attribute extension assign // simple assignment, mainly used for basic data type copy // create a new object, the new object and the old object are independent two objects, retain // Add the object counter to 1 readonly //, indicating that the read-only attribute will only generate the getter method and will not generate the setter method readwrite // default value, the setter and getter methods for table generation are nonatomic // non-atomic access without synchronization. multi-thread concurrent access improves performance (multi-thread protection to prevent reading by another thread before writing, data error.) A common condition is that the attribute name is the same as the instance variable name of the supported attribute. However, sometimes you may want the instance variable to be a name, the public attribute is another name. Copy the code // header file definition # import "Tire. h "// declaration and definition of Tire classes adapted to all weather conditions @ interface AllWeatherTire: Tire {NSString * tireName;} @ property float rainHanding; @ property float snowHanding; // It is used to name my tires @ property (copy) NSString * name; // implementation # import "AllWeatherTire. h "@ implementation AllWeatherTire @ synthesize name = tireName;-(NSString *) description {NSString * desc; desc = [NSString stringWithFormat: @" AllWeatherTier: name is % @, %. 1f, %. 1f, %. 1f, %. 1f ", self. name, self. pressure, self. treadDepth, self. rainHanding, self. snowHanding]; return desc;} copy the code here. The Compiler still creates the-setName: And-name methods, but in its implementation code, tireName instance variables are used. However, some errors may occur during compilation. Because the instance variables we directly access have been modified. You can use the name searched and replaced to solve the problem, or change the direct call of instance variables to the access method. For example, in the init method, change name = @ "Car" to self. name = @ "Car ". sometimes it is better to do it yourself. We have mentioned that attributes are variable-based, and the compiler will create setter and getter methods for you. But what if you don't want variables, setter, and getter methods? The answer is to use the @ dynamic keyword. If you declare the dynamic attribute and attempt to call the getter and setter methods that do not exist, you will get an error. Copy the code @ property (readonly) float bodyMassIndex; @ dynamic bodyMassIndex;-(float) bodyMassIndex {//;} // If the setter and getter methods that do not exist are called, an error is returned. The disadvantage of copying Code attributes is that attributes are not omnipotent. If the method is not suitable for a small range of attributes. The property can only replace the-setBlah and-blah methods, but does not support methods that need to receive additional parameters. For example, the Code of the tire object in the car object. -(Void) setTire :( Tire *) tire atIndex :( int) index;-(Tire *) tireAtIndex :( int) index; Summary of attributes this article mainly introduces attributes. When performing common operations on object variables, you can use attributes to reduce the number of codes that need to be written and subsequently read. The @ propert pre-compiled instruction can tell the compiler: "Hey, this object has these types of features" You can also let attributes pass other information, such as variability (read-only or read/write ). The compiler automatically generates the setter and getter methods of object variables in the background. Use the @ sythesize pre-compiled command to notify the compiler to generate an access method. You can also control which instance variables the access method generated by the compiler takes effect. If you do not want to use the default behavior, you can write your own access method. You can also use the @ dynamic command to tell the compiler not to generate variables and code. Although the dot expression usually appears in code with attributes, it is only a convenient way to call the setter and getter methods of the object. The dot expression method reduces the number of characters to be typed and further facilitates programmers who have used other languages.