[Study Notes-Objective-C] Chapter 10 object initialization and objectivec initialization in Objective-C-Basic tutorial 2nd
10.1 allocate objects
When an alloc message is sent to a class, a large enough memory can be allocated for the class to store all instance variables of the class. At the same time, alloc initializes all the memory areas to 0.
The newly allocated object cannot be used immediately and needs to be initialized first.
Initialization: obtains a memory storage object from the operating system.
The init method returns the object it is initializing.
10.11 initialization object
Note: The objects returned by the initialization method may be different from the allocated objects. For example, the string initialization function may decide to create an object of different classes.
10.12 compile the initialization method
// Let the superclass complete its own initialization work self = [super init] // update self so that the reference of the subsequent instance variables can be mapped to the correct memory location
If (self = [super init]) // if init returns nil, the object cannot be initialized.
Note: assigning values and determining conditions are a common technique.
10.13 what to do during initialization
Two different initialization methods:
1. The init method creates other objects contained in this object.
2. The init method only creates the object itself.
Lazy evaluation: Even if you have not set a custom attribute value, you should wait until the caller needs to create an object.
10.2 convenient initialization functions
Convenience initializer: the initialization method used to complete some additional work, which can reduce the burden.
If the object must be initialized with certain information, you should add part of the Information seat init method.
10.3 more component improvements
Change the tire array to NSMutableArray.
@interface Car : NSObject{ NSMutableArray *tires;}-(void)setTire: (Tire *) tire atIndex: (int) index;-(Tire *) tireAtIndex: (int) index;
Init method:
-(Id) init {if (self = [super init]) {tires = [[NSMutableArray alloc] init]; int I; for (I = 0; I <4; I ++) {[tires addObject: [NSNull null]; // Add four NSNull objects} // an array of four elements is generated upon initialization} return (self );} // init
Access Method:
-(Void) setTire: (Tire *) tire atIndex: (int) index {[tires replaceObjectAtIndex: index withObject: tire]; // when each tire is destroyed, all objects in the array are released and tire is cleared}
-(Tire *) tireAtIndex: (int) index {Tire * tire; tire = [tires objectAtIndex: index]; return (tire); // can be simplified: return ([tires objectiveAtIndex: index]);}
10.4 Car class memory cleanup
Original tire object creation method:
Tire *tire; tire = [[Tire alloc] init]; [tire setPressure: 23 + i]; [tire setTreadDepth: 33 - i];
Redefinition of 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
New init method implementation:
- (id) initWithPressure: (float) p treadDepth: (float) td { if (self = [super init]) { pressure = p; treadDepth = td; } return (self); }
New Way to create tire object:
Tire *tire; tire = [[Tire alloc] initWithPressure: 23 + i treadDepth: 33 - i];
10.5 specify the initialization Function
Four initialization methods of the tire class:
- (id) init { if (self = [super init]) { pressure = 34.0; treadDepth = 20.0;} return (self);} // init- (id) initWithPressure: (float) p{ if (self = [super init]) { pressure = p; treadDepth = 20.0;} return (self);} // initWithPressure- (id) initWithTreadDepth: (float) td{ if (self = [super init]) { pressure = 34.0; treadDepth = td;```} return (self);} // initWithTreadDepth- (id) initWithPressure: (float) p treadDepth: (float) td{ if (self = [super init]) { pressure = p; treadDepth = td; } return (self);} // initWithPressure:treadDepth:
New Tire class initialization method:
- (id) init{ if (self = [self initWithPressure: 34 treadDepth: 20]) { } return (self);} // init- (id) initWithPressure: (float) p{ if (self = [self initWithPressure: p treadDepth: 20.0]) { } return (self);} // initWithPressure- (id) initWithTreadDepth: (float) td{ if (self = [self initWithPressure: 34.0 treadDepth: td]) { } return (self);} // initWithTreadDepth- (id) initWithPressure: (float) p treadDepth: (float) td{ if (self = [super init]) { pressure = p; treadDepth = td; } return (self);} // initWithPressure:treadDepth:
Declaration file of the Tire subclass:
-(Id) initWithPressure: (float) p treadDepth: (float) td {// write if (self = [super initWithPressure: p treadDepth: td]) {rainHandling = 23.7; snowHandling = 42.5;} return (self);} // initWithPressure: treadDepth
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.