1.)直接上代码
文件中代码
@interface Person :NSObject
@propertyint age;
@property int weight;
@property NSString *name;
@end
实现文件代码
@implementation Person
@synthesize age;
@synthesize weight;
@synthesize name;
@end
这样我们可以将声明代码简化成这样:
@interface Person : NSObject
@property int age,weight;
@property NSString *name;
@end
这样我们可以将声明代码简化成这样:
@implementation Person
@synthesize age,weight,name ;
@end
为什么@property只可以将类型相同的变量写在一起,而@synthesize可以将所有的变量写在一起?
原因:@property直接与函数的声明和实现挂钩,声明和实现就包含来接受参数的数据类型,所以@property只可以把数据类型相同的变量写在一起;@synthesize只与自动生成的变量名有关,而不用去管数据类型,所以@synthesize可以将所有变量写在一起