.@property 是什么?
@perperty 是声明属性的语法,他可以快速方便的为实例变量创建存取器,并允许我们通过点语法使用存取器
【存取器:用于获取和设置实例变量的方法,获取实例变量值得是getter,设置实例变量存取器的是setter】
.手工创建存取器
#import <Foundation/Foundation.h>
@interface Car : NSObject
{
NSString *carName;
NSString *carType;
}
@property(nonatomic,strong) NSString *carName;
@property(nonatomic,strong) NSString *carType;
@property(nonatomic,strong) NSString *carNum;
@end
上面的carName和carType就是类Car的属性变量,可以看到分别对这两个实例变量声明了get/set方法,即存取器
#import "Car.h"
@implementation Car
@synthesize carName;
@synthesize carType;
@end
以上代码对存取器进行了实现
#import <Foundation/Foundation.h>
#import "car.h"
int main(int argc, const char * argv[])
{
@autoreleasepool {
Car *car = [[Car alloc] init];
car.carName = @"Defuli";
car.carType = @"SUB";
car.carNum = @"";
NSLog(@"The Car name is %@ and the type is %@ and the No is %@.",car.carName,car.carType,car.carNum);
[car setCarName:@"FUjian"];
[car setCarType:@"FLL"];
NSLog(@"The Car name is %@ and the type is %@",car.carName,car.carType);
}
return ;
}
mian中的实际用法