Objective-C 2.0Medium,
In the interface file, that is, afterThe suffix name is. HFile, use@ PropertyComeIdentifierAttribute (generallyInstance variables);
InImplementationFileIn the. M file, @ synthesize is used to identify the declared attribute, so that the system will automatically generate the setter and getter methods.
The following is an example.Program:
# Import <Cocoa/cocoa. h> @ interface useproperty: nsobject {int intx; int inty ;}@ property int intx, inty; @ end @ implementation useproperty @ synthesize intx, inty;-(void) print {nslog (@ "sum of the two numbers: % I", intx + inty);} @ endint main (INT argc, const char * argv []) {NSAID utoreleasepool * Pool = [[NSAID utoreleasepool alloc] init]; useproperty * testproperty = [[useproperty alloc] init]; [testproperty setintx: 2]; [testproperty setinty: 3]; [testproperty print]; [pool drain]; return 0 ;}
Note: In the preceding example, we have not defined the setintx and setinty methods, but can use them. This is the benefit of using property. Is it better than writing setter, the getter method is much more convenient... xi
Common attribute values in the attribute list and their meanings:
AttributeDescription
Assign Use a simple value assignment statement to set values for instance variables
Copy Use the copy method to set the value of instance variables
Noneatomic Return Value directly. If this attribute is not declared, it is the atomic attribute, and the storage of the instance variables is mutually exclusive. In an environment without garbage collection, the system retain the instance variable and sets
Autorelease Then return value
Readonly The instance variable value cannot be set. The Compiler does not generate the setter method.
Readwrite You can obtain and set instance variable values. In the implementation class file, use @ synthesize to automatically generate the setter and getter methods by the compiler. This is the case in the above example.
Retini Execute the retain operation when assigning values.
Getter = Name The setter method uses the name specified by name instead of the name of the instance variable.
Setter = Name The getter method uses the name specified by name instead of the name of the instance variable.
It's so sleepy. I have to wash and sleep. I will continue to learn the basic syntax of objective-C tomorrow.