新的特性显著减少了必须编写的冗长代码
Objective-C 2.0特性只适用于10.5 Leopard之后的版本,特性主要应用于Cocoa的新组件和iPhone之中
一、修改特性值
1、简化代码
举例代码:
#import <Foundation/Foudation.h>
#import "Tire.h"
@interface AllWeatherRadial : Tire{
float rainHandling;
float snowHandling;
}
@property float rainHandling;
@property float snowHandling;
@end//AllWeatherRadial
@property是一种新的编译器,表示生命了一个新对象的属性#import "Tire.h"
@interface AllWeatherRadial : Tire{
}
@property float rainHandling;
@property float snowHandling;
@end//AllWeatherRadial
@property预编译指令的作用是自动声明属性的setter和getter方法
@property float rainHandling;
@implementation AllWeatherRadial
@synthesize rainHandling;
@synthesize snowHandling;
- (id) initWithPressure: (float) p
treadDepth: (float) td
{
if (self = [super initWithPressure: p
treadDepth: td]) {
rainHandling = 23.7;
snowHandling = 42.5;
}
return (self);
} // initWithPressure:treadDepth
- (NSString *) description
{
NSString *desc;
desc = [[NSString alloc] initWithFormat:
@"AllWeatherRadial: %.1f / %.1f / %.1f / %.1f",
[self pressure], [self treadDepth],
[self rainHandling],
[self snowHandling]];
return (desc);
} // description
@end // AllWeatherRadial
@synthesize表示创建该属性的访问器@synthesize rainHandling;
@synthesize snowHandling;
- (id) initWithPressure: (float) p
treadDepth: (float) td
{
if (self = [super initWithPressure: p
treadDepth: td]) {
rainHandling = 23.7;
snowHandling = 42.5;
}
return (self);
} // initWithPressure:treadDepth
- (NSString *) description
{
NSString *desc;
desc = [[NSString alloc] initWithFormat:
@"AllWeatherRadial: %.1f / %.1f / %.1f / %.1f",
[self pressure], [self treadDepth],
[self rainHandling],
[self snowHandling]];
return (desc);
} // description
@end // AllWeatherRadial
遇到代码@synthesize rainHandling;时编译器将输出对应的-setRainHandling和-rainHandling方法的已编译代码
2、点表达式的应用
Objective-C 2.0引入了新的语法特性,使得C++和Java编程人员可以更容易上手
即使用对象名.属性名来赋值、取值
举例代码:
Tip:如果在使用特性时遇到了奇怪的错误提示,例如访问的对象不是struct类型,请检查是否已经包含了实用类所需的所有必要的头文件
二、特性扩展
#import <Cocoa/Cocoa.h>
@class Tire;
@class Engine;
@interface Car : NSObject {
NSString *name;
NSMutableArray *tires;
Engine *engine;
}
@property (copy) NSString *name;
@property (retain) Engine *engine;
- (void) setTire: (Tire *) tire
atIndex: (int) index;
- (Tire *) tireAtIndex: (int) index;
- (void) print;
@end // Car
@property已经取代了属性的访问器@class Tire;
@class Engine;
@interface Car : NSObject {
NSString *name;
NSMutableArray *tires;
Engine *engine;
}
@property (copy) NSString *name;
@property (retain) Engine *engine;
- (void) setTire: (Tire *) tire
atIndex: (int) index;
- (Tire *) tireAtIndex: (int) index;
- (void) print;
@end // Car
Tip:如果不在多线程环境,可以使用nonatomic声明这种声明可以提高访问器方法的调用速度(通常iPhone开发人员在资源有限的设备上使用以获得更好的性能)。
1、名称的使用
@interface Car : NSObject {
@property (copy) NSString *name;
@property (retain) Engine *engine;
@synthesize name = appellation;
可以将appellation重命名为其他名字,只需要修改实例变量的名称以及在@synthesize中使用的名称
NSString *appellation;
NSMutableArray *tires;
Engine *engine;
}NSMutableArray *tires;
Engine *engine;
@property (copy) NSString *name;
@property (retain) Engine *engine;
@synthesize name = appellation;
这种写法请使用对象.name调用避免歧义
2、只读特性
使用@property可以限制只读,readwrite可读写(一般不会写默认属性),readonly(只读)
举例代码:
当使用只读特性时,编译器将只为该属性生成getter而不生成setter方法
3、特性不是万能的
需要接收参数的方法不使用特性,特性只能替代最普通的赋值方法
举例代码:
- (void) setTire: (Tire *) tire
- (Tire *) tireAtIndex: (int) index;