1.allocation 的含义。
allocate被发送到类之后,这个类会申请足够的空间来创建一个对象,并且这个对象中所有的变量的值都将被设置成为0,或者nil,或者NO.
allocate的意思是拨出一段空间。
这个时候,由于所有的变量都是空的或者为0,这个时候我们就需要同时再调用init来给变量赋初值。
例如:Car *car=[[Car alloc] init];
2.Initialization methods 的创建
(id) init
{
if(self=[super init])
{
human = [Human new];
}
return (self);
} //init
3.用代码来举一个例子,包括类的声明和相关对象的初始化。
#import <Cocoa/Cocoa.h>
@class Tire;
@class Engine;
@interface Car : NSObject
{
NSMutableArray *tires;
Engine *engine;
}
- (void) setEngine: (Engine *) newEngine;
- (Engine *) engine;
- (void) setTire: (Tire *) tire
atIndex: (int) index;
- (Tire *) tireAtIndex: (int) index;
- (void) print;
@end // Car
We’ve upgraded pretty much every method ofCar to follow the memory management rules. Let’sstart withinit:
- (id) init{
if (self = [super init])
{
tires = [[NSMutableArray alloc] init];
for (int i = 0; i < 4; i++)
{
[tires addObject: [NSNull null]];
}
}
return (self);
} // init
这样跳过的部分就是第九章的异常部分,第十章除了正常的initialization部分之后的部分。
实际上从第11章开始,后边的每一章大概都是15页左右的内容。大概一天应该能全部看完,而且都比较重要。