第十一章特性


水果公司在Objective-C 2.0中引入了特性(property),它组合了新的预编译指令和新的属性访问器语法
新的特性显著减少了必须编写的冗长代码

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是一种新的编译器,表示生命了一个新对象的属性
@property预编译指令的作用是自动声明属性的setter和getter方法
@property float rainHandling;
          语句表示AllWeathorRadial类的对象有float类型的属性,其名称为rainHandling
          可以通过-setRainHandling来设置属性
          可以通过-rainHandling来访问属性
        
          实现部分
      #import "AllWeatherRadial.h"
@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;时编译器将输出对应的-setRainHandling和-rainHandling方法的已编译代码

2、点表达式的应用
Objective-C 2.0引入了新的语法特性,使得C++和Java编程人员可以更容易上手
即使用对象名.属性名来赋值、取值
举例代码:
    旧的赋值代码
         [tire setRainHandling: 20 + i];
         [tire setSnowHandling: 28 + i];
    新的赋值代码
         tire.rainHandling = 20 + i;
         tire.snowHandling = 28 + i;
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已经取代了属性的访问器
         向name对象添加了copy方法,通知编译器和类的使用者name对象将被复制
         声明了engine对象只有保留和释放操作
         如果不指定操作,编译器将默认执行赋值操作
Tip:如果不在多线程环境,可以使用nonatomic声明这种声明可以提高访问器方法的调用速度(通常iPhone开发人员在资源有限的设备上使用以获得更好的性能)。
     如果不希望保留属性的对象,可以使用assign方法以避免保留周期问题

1、名称的使用
@interface Car : NSObject {
NSString *appellation;
NSMutableArray *tires;
Engine *engine;
}
@property (copy) NSString *name;
@property (retain) Engine *engine;


@synthesize name = appellation;
可以将appellation重命名为其他名字,只需要修改实例变量的名称以及在@synthesize中使用的名称
这种写法请使用对象.name调用避免歧义
2、只读特性
使用@property可以限制只读,readwrite可读写(一般不会写默认属性),readonly(只读)
举例代码:
      @property (readwrite, copy) NSString *name;
      @property (readonly) float shoeSize;
当使用只读特性时,编译器将只为该属性生成getter而不生成setter方法
3、特性不是万能的
需要接收参数的方法不使用特性,特性只能替代最普通的赋值方法
举例代码:
- (void) setTire: (Tire *) tire
         atIndex: (int) index;
- (Tire *) tireAtIndex: (int) index;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值