Reprinted please indicate the source: blog.csdn.net/zhangxingping
Attribute Declaration
The attribute Declaration uses the keyword @ property. The attribute declaration can be any part of the method declaration block in the @ interface class. @ Property can also be in protocal and category. The general form is as follows:
@ Property (attributes) type name
Because both property and attribute have attributes in Chinese, to avoid confusion, the two words are not translated for the moment. Attributes is used to describe the property with name; type indicates the type of the property with name, the type here can be any objective-C class and POD type (for more information about pod see: http://www.fnal.gov/docs/working-groups/fpcltf/Pkg/ISOcxx/doc/POD.html
) Type; name indicates the name of the property.
Attribute can be a set of description information separated by commas (,). This set of description information is used together to modify the property named name. The descriptions can be divided into the following types:
1. access method names
The default setter () method name is set + attribute name;
The default getter () method name is the property name;
You can specify the access method name in the following ways:
Specify the setter () method name: setter = method name. The method must have only one parameter, and the parameter type is the same as the property type; the return type of the method must be void.
Specify the name of the getter () method: getter = method name. The return value of the method must be of the same type as that of the property, and the return value is not required.
2. read/write Property
By default, property can be read and written: readwrite, that is, the getter and setter methods exist. You can also specify the property as readonly, that is, the property is read-only. In this case, the property should not have the setter method. (I have never seen the writeonly property, probably because it is of no practical use .)
3. Some details about setter
The following keywords describe how to set the property value for the setter method:
Assign
It indicates that the setter method only performs simple value assignment. This is the default one. Generally, assign is used to describe the scalar type and objects that do not belong to this class. For example, delegate. (Scalar, scalar type is relative to aggregate type. Scalar type represents a simple value. For example, the built-in data types of the language are basically scalar types. Aggregate type represents multiple values of the same type or different types, such as arrays, struct, and classes .)
Retain
Indicates that the setter method should send a retain message to the new value, or call the retain method of the new value. The old value will be sent with a release message.
Copy
It indicates that the copy assignment should be performed during the value assignment in the setter method. The old value will be sent with a release message. Assign a value to the property by calling the copy method of the new value. This assignment method applies only to object types and requires that the object complies with the nscopying protocol.
These keywords are mutually exclusive, that is, only one type can be used at the same time.
4. multithreading (Atomicity)
The nonatomic keyword can be used to indicate that the property is non-atomic. By default, property is atomic, which provides secure access to data in a multi-threaded environment.
In addition, the iboutlet keyword is used to indicate that the property is an outlet of interface builder. It is not a formal property attribute.
The type of the attribute name in the previous program is nsstring *. This is not a basic data type, and the name attribute itself belongs to the Student Class Object, therefore, by default, the setter method of name will use the Assign Method for value assignment, which is not reasonable. It is more reasonable to assign values using retain. Modify the above program as follows:
Student. h:
# Import <Foundation/Foundation. h> @ interface Student: nsobject {@ private nsstring * Name; // Student name float math; // float English; // english score} // Add the retain keyword. According to the basic principle of memory management, the release message should be sent to name @ property (retain) nsstring * name during dealloc; @ property float math; @ property float English;-(ID) initwithname :( nsstring *) aname math :( float) scoremath English :( float) scoreenglish; //-(nsstring *) getname; //-(float) getmath; //-(float) getenglish; @ end
Student. m is as follows:
# Import "student. H "@ implementation student @ synthesize name; @ synthesize math; @ synthesize English;-(ID) Init {self = [Super init]; If (Self) {name = nil; math = 0; English = 0;} return self;}-(ID) initwithname :( nsstring *) aname math :( float) scoremath English :( float) scoreenglish {self = [Super init]; If (Self) {name = aname; math = scoremath; English = scoreenglish;} return self;} //-(nsstring *) Ge Tname // {// return name; //} //-(float) getmath // {// return math; //} //-(float) getenglish // {// return English; //} //-(void) dealloc // {// [Super dealloc]; //}-(void) dealloc {[Name Release]; // send the release message to name when the object is destroyed. [Super dealloc];} @ end
Main. m remains unchanged. At this time, the compilation warning has disappeared. The program output result is as follows:
Name: Tony
Math: 99.000000
Math: 89.980003
The result is correct.