Reprinted please indicate the source: blog.csdn.net/zhangxingping
About attributes
In C ++, you usually need to write the getter and setter methods to obtain or set the value of the instance variable. The two methods need to be displayed in the program. This method is also applicable in objective-C. However, objective-C provides a more convenient way to complete this function. It is an attribute. Compared with the getter and setter Methods displayed in C ++, the property mechanism makes the getter function and setter easier and easier.
Attributes in objectivc-C are declared by the keyword @ property.
For example, a class is as follows:
Student. h file:
# Import <Foundation/Foundation. h> @ interface Student: nsobject {@ private nsstring * Name; // Student name float math; // score of the Mathematics Subject float English; // score of the English subject}-(ID) initwithname :( nsstring *) aname math :( float) scoremath English :( float) scoreenglish;-(nsstring *) getname;-(float) getmath;-(float) getenglish; @ end
Student. M file:
#import "Student.h" @implementation Student - (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 *)getName{ return name;} -(float)getMath{ return math;} -(float)getEnglish{ return english;} - (void)dealloc{ [super dealloc];} @end
The main. M file is as follows:
# Import <Foundation/Foundation. h>
#import "Student.h" int main (int argc, const char * argv[]){ NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; Student * p = [[Student alloc] initWithName:@"Mark" math:80.0f english:100.00f]; NSLog(@"Name:%@",[p getName]); NSLog(@"Math:%f",[p getMath]); NSLog(@"Math:%f",[p getEnglish]); [p release]; [pool drain]; return 0;}
The output result of the above program is as follows:
Name: Mark
Math: 80.000000
Math: 100.000000
The above program uses the method in C ++ to compile the getter method to obtain the value of the private instance variable. Although the implementation of these getter methods is simple, it must be completed by the programmer's display and writing. After the property mechanism is applied, the program is as follows:
Student. h file:
# Import <Foundation/Foundation. h> @ interface Student: nsobject {@ private nsstring * Name; // Student name float math; // float English; // english score} @ property nsstring * Name; @ 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 file:
#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 *)getName//{// return name;//}////-(float)getMath//{// return math;//}////-(float)getEnglish//{// return english;//}////- (void)dealloc//{// [super dealloc];//} @end
The main. M file is as follows:
#import <Foundation/Foundation.h>#import "Student.h" int main (int argc, const char * argv[]){ NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; Student * p = [[Student alloc] initWithName:@"Mark" math:80.0f english:100.00f]; // NSLog(@"Name:%@",[p getName]);// NSLog(@"Math:%f",[p getMath]);// NSLog(@"Math:%f",[p getEnglish]); NSLog(@"Name:%@",p.name); NSLog(@"Math:%f",p.math); NSLog(@"Math:%f",p.english); [p release]; [pool drain]; return 0;}
The program output is as follows:
Name: Mark
Math: 80.000000
Math: 100.000000
The visible property mechanism makes the program more concise and clear.
The above Program introduces a new keyword @ synthesize, which tells the compiler to automatically generate the getter () and setter () methods for the attributes following it. Note that although the description applies to the "automatically generate the getter () and setter () methods", we do not actually see the generated code. The getter Methods automatically generated by the compiler are: name (), math (), English (), and Sette Methods: setname (), setmath (), and setenglish () is the same as a normal method. For example, we can modify the above main () function as follows:
#import <Foundation/Foundation.h>#import "Student.h" int main (int argc, const char * argv[]){ NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; Student * p = [[Student alloc] initWithName:@"Mark" math:80.0f english:100.00f]; // NSLog(@"Name:%@",[p getName]);// NSLog(@"Math:%f",[p getMath]);// NSLog(@"Math:%f",[p getEnglish]); // NSLog(@"Name:%@",p.name);// NSLog(@"Math:%f",p.math);// NSLog(@"Math:%f",p.english); [p setName:@"Tony"]; [p setMath:99.0f]; [p setEnglish:89.98f]; NSLog(@"Name:%@",[p name]); NSLog(@"Math:%f",[p math]); NSLog(@"Math:%f",[p english]); [p release]; [pool drain]; return 0;}
Program output:
Name: Tony
Math: 99.000000
Math: 89.980003
After the above program is compiled in xcode, a yellow exclamation point will be displayed in the header of @ property nsstring * name; the line where the code is located, which indicates that the compiler provides a compilation warning in the code of this line. Click the yellow exclamation mark to see the warning message: "No 'assign', 'reta ', or 'copy' attribute is specified-'assign' is assumed "and" Default property attribute 'assign' not appropriate for non-GC object ". The warning information indicates: "We didn't explicitly specify whether it should be assign, retain or copy, but saved assign" and "the default attribute setting assign is not suitable for non-GC objects.
", What are the meanings of the two warning messages? What are the meanings of assign, retian, and copy? What is a GC object? What are non-GC objects? For these questions, see the description below.