@synthesize 属性名称 = 实例变量名称;
例如,之前的程序可以修改为:
Student.h:
#import <Foundation/Foundation.h> @interface Student : NSObject { @public //将下面的三个实例变量修改为是公有的,以便输出其值,和属性的值进行对比 NSString * name; //学生的姓名
float math; //数学科目的成绩 float english; //英语科目的成绩 }//@property (retain) NSString * name;@property (retain) NSString * fullName;@property float math;@property float english; -(id)initWithName:(NSString*)aName math:(float)scoreMath english:(float)scoreEnglish;@end
Student.m:
#import "Student.h" @implementation Student //@synthesize name; //为fullName属性指定实例变量为name,以后对fullName的操作相当于是对name的操作 @synthesize fullName = 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; } -(void) dealloc { [super dealloc]; } @end
main.m:
#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 fullName]); //输出属性的值 NSLog(@"Name:%@",p->name); //输出实例变量的值 NSLog(@"Math:%f",[p math]); NSLog(@"English:%f",[p english]); //[p setName:@"Tony"]; [p setFullName:@"Tony"]; //设置属性的值 [p setMath:99.0f]; [p setEnglish:89.98f]; NSLog(@"Name:%@",[p fullName]); //输出属性的值 NSLog(@"Name:%@",p->name); //输出实例变量的值 NSLog(@"Math:%f",[p math]); NSLog(@"English:%f",[p english]); [p release]; [pool drain]; return 0; }
上面程序的输出如下:
Name:Mark
Name:Mark
Math:80.000000
English:100.000000
Name:Tony
Name:Tony
Math:99.000000
English:89.980003
可见这种为属性指定实例变量的方法使得对属性的操作变成了对实例变量的操作。那么在之前的程序中,我们并没有为属性指定实例变量,此时对属性的操作也是相当于对实例变量的操作嘛?如果是,那是对那些实例变量的操作呢?我们来看看下面程序的运行结果:
Student.h文件:
Student.m文件:#import <Foundation/Foundation.h> @interface Student : NSObject { @public NSString * name; //学生的姓名 float math; //数学科目的成绩 float english; //英语科目的成绩 } //下面三个属性的类型,名称和类的三个实例变量的类型,名称完全相同 @property (retain) NSString * name; @property float math; @property float english; -(id)initWithName:(NSString*)aName math:(float)scoreMath english:(float)scoreEnglish; @end
main.m文件:#import "Student.h" @implementation Student //synthesize 指令并没有为这三个属性指定实例变量 @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; } -(void) dealloc { [name release]; [super dealloc]; } @end
#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 name]); NSLog(@"Name:%@",p->name); NSLog(@"Math:%f",[p math]); NSLog(@"Math:%f",p->math); NSLog(@"English:%f",[p english]); NSLog(@"English:%f",p->english); [p setName:@"Tony"]; [p setMath:99.0f]; [p setEnglish:89.98f]; NSLog(@"Name:%@",[p name]); NSLog(@"Name:%@",p->name); NSLog(@"Math:%f",[p math]); NSLog(@"Math:%f",p->math); NSLog(@"English:%f",[p english]); NSLog(@"English:%f",p->english); [p release]; [pool drain]; return 0; }
上面程序的输出为:
Name:Mark
Name:Mark
Math:80.000000
Math:80.000000
English:100.000000
English:100.000000
Name:Tony
Name:Tony
Math:99.000000
Math:99.000000
English:89.980003
English:89.980003
可见在使用@synthesize指令的时候,如果没有为属性指定实例变量,并且属性的类型和名称与实例变量的类型和名称相同的情况下,会自动为该属性指定了同名的实例变量。那如果没有为属性指定实例变量,且类中没有与该属性类型和名称都相同的实例变量,会出现什么样的情况了?例如,将上述程序中对name属性的声明,实现以及调用都修改为Name,完整程序如下:
Student.h文件
#import <Foundation/Foundation.h> @interface Student : NSObject { @public NSString * name; //学生的姓名 float math; //数学科目的成绩 float english; //英语科目的成绩 } @property (retain) NSString * Name; //此处Name首字母为大写,以便和类的实例变量name进行区分 @property float math; @property float english; -(id)initWithName:(NSString*)aName math:(float)scoreMath english:(float)scoreEnglish; @end
Student.m文件:
#import "Student.h" @implementation Student @synthesize Name; //此处Name首字母为大写,以便和类的实例变量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; } -(void) dealloc { [Name release]; [super dealloc]; } @end
main.m文件:
#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 Name]); NSLog(@"Name:%@",p->name); NSLog(@"Math:%f",[p math]); NSLog(@"Math:%f",p->math); NSLog(@"English:%f",[p english]); NSLog(@"English:%f",p->english); [p setName:@"Tony"]; [p setMath:99.0f]; [p setEnglish:89.98f]; NSLog(@"Name:%@",[p Name]); NSLog(@"Name:%@",p->name); NSLog(@"Math:%f",[p math]); NSLog(@"Math:%f",p->math); NSLog(@"English:%f",[p english]); NSLog(@"English:%f",p->english); [p release]; [pool drain]; return 0; }
上面程序的输出如下:Name:(null)
Name:Mark
Math:80.000000
Math:80.000000
English:100.000000
English:100.000000
Name:Tony
Name:Mark
Math:99.000000
Math:99.000000
English:89.980003
English:89.980003
从输出结果可以看出显然属性Name和实例变量name不是同一个对象。注意,上面的代码在objectivc-c2.0 以前的版本上可能会报告错误。这是因为之前的版本中要求属性必须指定对应的实例变量。如果不显示指定对应的实例变量,则会以类中同名的实例变量对应,如果没有同名的实例变量,编译就会报告错误。在0bjective-2.0版本中取消了这个限制,因此上面的程序是可以编译通过并且运行的。只是在没有显示指定属性对应的实例变量的时候,也没有同名实例变量的时候,属性单独存在不和任何的实例变量对应。因此上面程序输出结果中的Name:一项,属性和实例变量的值是不一样的。
除了@synthesize指令外,还有一个用于实现属性的指令:@dynamic。这个指令告诉编译器我们会显示地实现该属性的getter和setter方法,或者是该属性的getter和setter方式是通过别的方式来完成的,比如动态加载代码以及动态方法解析等方式。如果实现属性时使用的是@dynamic指令,却没有通过上述的方式来实现该属性的getter和setter方法,编译是不会报告错误的,但程序会在运行的时候出错。例如下面的程序:
Student.h:
#import <Foundation/Foundation.h> @interface Student : NSObject { @public NSString * name; //学生的姓名 float math; //数学科目的成绩 float english; //英语科目的成绩 } @property (retain) NSString * fullName; @property float math; @property float english; -(id)initWithName:(NSString*)aName math:(float)scoreMath english:(float)scoreEnglish; @end
Student.m
#import "Student.h" @implementation Student @dynamic fullName; @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; } -(void) dealloc { // [name release]; [super dealloc]; } //-(NSString *)fullName //{ // return name; //} // // //-(void) setFullName:(NSString *)aName //{ // [aName retain]; // [name release]; // name = aName; //} @end
main.m:
#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]; //程序运行时,会在该行处报告异常:unrecognized selector sent to instance 0Xxxxxx NSLog(@"Name:%@",[p fullName]); NSLog(@"Name:%@",p->name); NSLog(@"Math:%f",[p math]); NSLog(@"Math:%f",p->math); NSLog(@"English:%f",[p english]); NSLog(@"English:%f",p->english); [p setFullName:@"Tony"]; [p setMath:99.0f]; [p setEnglish:89.98f]; NSLog(@"Name:%@",[p fullName]); NSLog(@"Name:%@",p->name); NSLog(@"Math:%f",[p math]); NSLog(@"Math:%f",p->math); NSLog(@"English:%f",[p english]); NSLog(@"English:%f",p->english); [p release]; [pool drain]; return 0; }
可见在使用@dynamic指令后,由于没有实现fullName的getter方法而导致程序异常。将Student.m中的getter和setter方法的注释//去掉,并将dealloc方法中的注释也去掉,再次编译运行程序结果如下:Name:Mark
Name:Mark
Math:80.000000
Math:80.000000
English:100.000000
English:100.000000
Name:Tony
Name:Tony
Math:99.000000
Math:99.000000
English:89.980003
English:89.980003