10.1 Assigning objects
Sending a ALLOC message to a class allocates a large enough memory for the class to hold all the instance variables of the class, while Alloc also initializes the memory area to 0.
Objects that have just been allocated are not immediately available and need to be initialized first.
Initialize: Gets a piece of memory storage object from the operating system.
The Init method returns the object it is initializing
10.11 Initializing objects
Note: The object returned by the initialization method may be different from the assigned object. Example: A string initialization function might decide to create an object of a non-homogeneous type.
10.12 Writing initialization methods
//让超类完成其自身的初始化工作self = [super init]//更新self,以便其后的实例变量的引用可以被映射到正确的内存位置
if(self = [super init])//如果init返回nil,表明未能初始化该对象
Note: It is a common technique to judge assignment and condition
10.13 What to do when initializing
Two different ways to initialize:
1. The Init method creates other objects that the object contains
2. The Init method only creates the object itself
Lazy Evaluation (Lazy evaluation): You should wait until the caller needs to create the object, even if you do not currently have a value for the custom property set.
10.2 Convenient initialization functions
Convenience initialization function (convenience initializer): The initialization method used to perform some extra work can relieve the burden.
If the object has to be initialized with some information, you should add a portion of the information to the seat Init method.
10.3 More Parts improvements
Change the tyre array form to Nsmutablearray form
@interface Car : NSObject{ *tires*) tire atIndex: (intindex*) tireAtIndex: (intindex;
Init method:
- (id) init { if (self = [super init]) { [[NSMutableArray alloc] init]; int i; for (i = 0; i < 4; i++) { [tires addObject: [NSNull null]]; //添加4个NSNull对象 } //初始化即生成4个元素的数组} return (self);} // init
Access method:
- (void) setTire: (Tire *) tire atIndex: (intindex{ index withObject: tire]; //以每个轮胎为单位 // 数组被销毁时,将释放数组中所有对象,tire也会被清理
*) tireAtIndex: (intindex{ *tire; index]; return (tire); //可简化为:returnindex
10.4 Car class Memory cleanup
The original way to create tire objects:
Tire *tire; tire = [[Tire alloc] init]; set23 + i]; setTread33 - i];
To redefine the Init method:
@interface Tire : NSObject { float pressure; float treadDepth; } - (id) initWithPressure: (float) pressure treadDepth: (float) treadDepth; - (void) setPressure: (float) pressure; - (float) pressure; - (void) setTreadDepth: (float) treadDepth; - (float) treadDepth;@end
The new Init method implements:
- (id) initWithPressure: (float) p treadDepth: (float) td { if (self = [super init]) { pressure = p; treadDepth = td; } return (self);
New ways to create tire objects:
Tire *tire; [[Tire alloc]23i33i];
10.5 Specifying initialization functions
Four initialization methods for the tire class:
- (ID) Init {if( Self= [SuperInit]) {pressure =34.0; Treaddepth =20.0;}return( Self);}//Init- (ID) Initwithpressure: (float) p{if( Self= [SuperInit]) {pressure = P; Treaddepth =20.0;}return( Self);}//Initwithpressure- (ID) Initwithtreaddepth: (float) td{if( Self= [SuperInit]) {pressure =34.0; treaddepth = TD; "}return( Self);}//Initwithtreaddepth- (ID) Initwithpressure: (float) P Treaddepth: (float) td{if( Self= [SuperInit]) {pressure = P; Treaddepth = TD; }return( Self);}//initwithpressure:treaddepth:
New initialization mode for tire class:
- (ID) init{if( Self= [ SelfInitwithpressure: theTreaddepth: -]) { }return( Self);}//Init- (ID) Initwithpressure: (float) p{if( Self= [ SelfInitwithpressure:p treaddepth:20.0]) { }return( Self);}//Initwithpressure- (ID) Initwithtreaddepth: (float) td{if( Self= [ SelfInitwithpressure:34.0TREADDEPTH:TD]) {}return( Self);}//Initwithtreaddepth- (ID) Initwithpressure: (float) P Treaddepth: (float) td{if( Self= [SuperInit]) {pressure = P; Treaddepth = TD; }return( Self);}//initwithpressure:treaddepth:
Declaration file for Tire subclasses:
- (id) initWithPressure: (float) p treadDepth: (float) td{ //根据父类的指定初始化函数编写 if (self = [super initWithPressure: p treadDepth: td]) { 23.7; 42.5; } return (self// initWithPressure:treadDepth
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
[Study note-objective-c] "objective-c-Basic Tutorial 2nd Edition" tenth chapter object initialization