Small Knowledge Supplement
First, ID
1. Introduction
A universal pointer that can point to any OC object, equivalent to NSObject *
Definition of ID Type
struct Objc_object { *ID;
2. Use
Note: Do not add * after ID
ID p = [person new];
3. Limitations
Call a non-existent method, the compiler will immediately error
Ii. extraction of files of. h and. m
1) Each class is distributed in different files
2) The declaration of the class is placed in the. h file, the implementation of the class is placed in the. m file
3) If you want to use a class, include the. h declaration file for a class
iii. addition of the Init method (emphasis)
1. Principle of object creation
New Split two-part song
U Allocate Memory (+ALLOC)
U initialization (-init)
Person *P1 =*p1 =*p = [[Person alloc] init];
2. Overriding the Init method
L WANT the member variable to have some default values right after the object is created
L Rewrite process of init method
-(ID) init{
1. Be sure to call Super's Init method: Initialize some member variables and other properties declared in the parent class
Self = [super init]; Current Object Self
2. If the initialization succeeds, it is necessary to proceed with the next initialization if (self = [super init]) {//Initialize success ; } return Self ;//3. Returns a value that has already been initialized}
3. Custom Construction methods
Some specifications of the L-structure method
-(ID) initwithage: (int) Age { if "self = [super init]") {= age ; } return Self ;}
Pass multiple parameters for initialization
-(ID) initwithage: (int) Age Andno: (int) No;
The return value is the ID type
Method names start with Init
OC for iOS Development (eight)--supplement to the Little Knowledge supplement Init method