Initialize object allocation memory + (ID) alloc;
- The birth of an object is essentially a large enough memory from the operating system to hold all the instance variables of the class and designate it as the location of the instance variable that holds the object.
- The Alloc method also sets all the memory area to 0. (because there are many problems with other languages not initialized), the result is that the BOOL variable has an initial value of no, all the int type variable is the 0,float variable is 0.0, all pointers are nil.
Initialize Object-(ID) init;
- The function of initialization is to ensure that our instance variables are available complete objects, such as: we assign values to other instance variables that are dependent on the class in initialization
- There are two types of initialization ideas. The first is that all dependent objects are created and assigned in the initialization method, "out of the box", and the second is the lazy evaluation, which is the first placeholder at initialization time, when used, in the confirmation of the specific value. These two approaches need to be trade-offs.
- How to write the initialization method:
- (id)init{ if (self = [super init]){ //do something init content... }}
Out-of-the-box initialization method
Personal understanding is to put the initialization process into the method, should be said to be encapsulated in the method, when used, only need to call this method to achieve the initialization of the object. and the Declaration and implementation of the Setter,getter method is not required at this time
defined directly in the corresponding implementation file:
- (id)init{ if (self = [super init]) { engine = [[QYEngine alloc] init]; tires = [[NSMutableArray alloc] initWithCapacity:4]; for (int04; i++) { QYTire *tire = [[QYTire alloc] init]; [tires addObject:tire]; [tire release]; } } returnself;}//开箱即用
Call is also very simple, this session is called in the main function can be
#import <Foundation/Foundation.h>#import "QYCar.h"#import "QYEngine.h"#import "QYTire.h"int main(intconstchar * argv[]) { @autoreleasepool { //开箱即用 QYCar *myCar = [[QYCar alloc] init]; NSLog(@"Car info: %@",myCar); [myCar release]; } return0;}
Of course, the code is not complete, just need to implement a partial printing function in the corresponding implementation file can be
The result of the output is as follows
Car info:i am Engine, (
"I am Tire",
"I am Tire",
"I am Tire",
"I am Tire"
)
2015-06-28 21:26:28.068 initdemo[2276:1416754] Car destory
2015-06-28 21:26:29.251 initdemo[2276:1416754] engine destory
2015-06-28 21:26:29.251 initdemo[2276:1416754] Tire destory
2015-06-28 21:26:30.187 initdemo[2276:1416754] Tire destory
2015-06-28 21:26:31.621 initdemo[2276:1416754] Tire destory
Lazy Load Initialization mode
Feeling lazy to load the way is more troublesome, he not only need to Setter,getter method declaration and definition, but also need to be in a specific invocation of the site initialization mode
Lazy loading initialization method is implemented in the corresponding implementation file:
//Lazy loading-(ID) init{self = [SuperINIT];if(self) {tires = [[Nsmutablearray alloc] Initwithcapacity:4]; for(inti =0; I <4; i++) {Tires[i] = [NSNullNULL]; } }returnSelf;} - (void) Setengine: (Qyengine *) newengine{[newengine retain]; [Engine release]; engine = Newengine;} -(qyengine*) engine{returnEngine;} - (void) Settire: (Qytire *) Newtire Atindex: (Nsuinteger)Index{The use of the //array causes the Tires reference counter to add 1//[newtire retain];//[Tires[index] release];tires[Index] = Newtire;} -(qytire*) Tireatindex: (Nsuinteger)Index{returntires[Index];}//Lazy loading
The code in Main () is also quite large, with the same output as above.
int main(intconstchar * argv[]) { @autoreleasepool { //懒加载 QYCar *myCar = [[QYCar alloc] init]; QYEngine *engine = [[QYEngine alloc] init]; [myCar setEngine:engine]; [engine release]; for (int04; i++) { QYTire *tire = [[QYTire alloc] init]; [myCar setTire:tire atIndex:i]; [tire release]; } NSLog(@"Car info: %@",myCar); [myCar release]; }
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Objective-c initialization