Objective-C基础教程(第2版)学习记录点滴

编译器功能

@property

@synthesize    xxx         表示“创建了该属性的访问代码”。当遇到@synthesize xxx;这行代码时,编译器将添加实现-setXxx:和-xxx方法的预编译代码。       该@synthesize预编译指令不同于代码生成。你永远看不到实现-setXxx:和-xxx的代码,但是这些方法确实存在并可以被调用。这种技术使苹果公司可以更加灵活地改变Objective-C中生成访问方法的方式,并获得更安全的实现和更高的的性能。在Xcode4.5以后的版本中,可以不必使用@synthesize了。

@synthesize (readOnly) xxx表示xxx该属性是只读的。

 

@dynamic指令来制定属性并告诉编译器不需要去创建变量或getter方法—我们可以自己来。

 

 

类别 是一种为现有的类添加新方法的方式。

 

利用Objective-C的动态运行时分配机制,可以为现有的类添加新方法。这些新方法在Objective C里被称为类别(category)。

程序员总是习惯把类别代码放在独立的文件中,通常会以“类名称+类别名称”的风格命名。不是硬性规定,但可以养成这个好习惯。

类别创建方法:选择File->New>New File选项(Command+N),弹出的新文件窗口选择Cocoa然后选择Objective-Ccategory。

      

@interface

The declaration of a category looks alot like the declaration for a class:

@interface NSString(NumberConvenience)

- (NSNumber *) lengthAsNumber;

@end // NumberConvenience

 

@implementation

It comes as no surprise that the @interfacesection has an @implementation companion. You put

the methods you’re writing in@implementation:

@implementation NSString(NumberConvenience)

- (NSNumber *) lengthAsNumber

{

NSUInteger length = [self length];

return ([NSNumber numberWithUnsignedInt:length]);

} // lengthAsNumber

@end // NumberConvenience

 

特殊的类别,类扩展(Class extension )。

 

Run循环是一种Cocoa概念,它在等待某些事情发生之前一直处于阻塞状态,即不执行任何代码。

 

协议

@protocol NSCopying

@protocol MySuperDuberProtocol <MyParentProtocol>

 

Cocoa中的非正式协议主键被替换成了带有@optional方法的正式协议。

委托delegation就是某个对象指定另一个对象处理某些特定任务的设计模式。

你可能感兴趣的:(Objective-C基础教程(第2版)学习记录点滴)