Objective-C 注意要点

Objective-C注意要点

1、没有C++中的bool值,对应的类型为BOOL,聚会为YES、NO.

2、定义

@interface Car

{

int m_nWeight;

int m_nSpeed;

}

@property int m_nWeight;

@proprety (readonly) int m_nSpeed;

// 名字关联

@property int speed;

@synthesize speed = m_nSpeed;

@end

3、实现

@implementation Car

@synthesize m_nWeight;

@synthesize m_nSpeed;

@end

4、调用

[self setSpeed:speed];

Car *pCar = [Car new];

Car *pCar = [[Car alloc] init];

5、没有private、protected、public访问控制,所有东西都是public

6、无法设置函数默认参数的特性

7、Categories特性使得我们可以给已有的类添加新的接口。

@interface Car (CarDescription)

@implementation Car (CarDescription)

在使用的时候,无需单独import Cardescription.h。

只要import 了Car.h 编译器会自己关联Car相关的categories。

注意:Category的使用要注意两点:一是Category中不能声明新的成员变量。

8、协议

@protocol NSCopying

@optional

@required

- (id) copyWithZone: (NSZone*) zone;

@end

@interface Car: NSObject <NSCopying>