IPhone applicationsLearning notesObjectThe initialization of is the content to be introduced in this article, mainly to learnObjectThe initialization content. Code is included for easy learning!
1. About self = [super init]
The first code to run is [super init], which enables the superclasses to complete their own initialization work. The class inherited from the root NSObject class calls the superclass initialization method, so that NSObject can perform any operation required, so that the object can respond to the message and process the reserved counter. The class inherited from other classes calls the superclass initialization method, so that the subclass has the opportunity to implement its own new initialization.
The distance between the memory location of the instance variable and the hidden self parameter is fixed. If a new object is returned from the init method, you must update self, so that the reference of any instance variable can be mapped to the correct memory location, which is why we need to assign values in the form of self = [super init.
The value assignment affects the self value in the init method, without any content beyond the scope of the method.
2. Convenient initialization functions
Many classes contain convenient initialization functions, which are used to complete initialization methods for some additional work.
NSString class:
- -id) init;
The above method initializes a new Null String. This method is of little use for immutable NSString classes. However, you can allocate and initialize a new NSMutableString Class Object and start saving characters to the object. You can use this object as follows:
- NSString *emptyString = [[NSString alloc] init];
The above code returns an empty string
Of course, you can also accept formatted strings and output the same formatted results.
- string = [[NSString alloc] initWithFormat:@"%d or %d", 25, 624];
The code above returns a string with a value of 25 or 624 ";
What's more powerful is that you can open a text file in the specified path, read the file content too much, and use the File Content to initialize a string.
- string = [[NSString alloc] initWithContentsOfFile: @"words.txt"];
To create an NSMutableArray array, replaceObjectAtIndex: withObject is a simple method. This method is most suitable for setTire: atIndex.
To use the replaceObjectAtIndex: withObject: method, an object that can be replaced must exist at the specified index position.
The new NSMutableArray array does not contain any content. Therefore, some objects need to be used as placeholders. The NSNull class objects are ideal for this purpose. Therefore, we add four NSNull objects to the array.
The code for attaching Tire. m is as follows:
- #import "Tire.h"
- @implementation Tire
- - (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]) {
- ppressure = p;
- treadDepth = td;
- }
- return (self);
- } // initWithPressure:treadDepth:
- - (void) setPressure: (float) p
- {
- ppressure = p;
- } // setPressure
- - (float) pressure
- {
- return (pressure);
- } // pressure
- - (void) setTreadDepth: (float) td
- {
- treadDepth = td;
- } // setTreadDepth
- - (float) treadDepth
- {
- return (treadDepth);
- } // treadDepth
- - (NSString *) description
- {
- NSString *desc;
- desc = [NSString stringWithFormat:
- @"Tire: Pressure: %.1f TreadDepth: %.1f",
- pressure, treadDepth];
- return (desc);
- } // description
- @end // Tire
Summary:IPhone applicationsStudy NotesObjectThe initialization is complete. I hope this article will help you!