First, the construction method
The constructor method in OC is the method at the beginning of Init, which initializes an object so that an object is created with some properties and values.
The constructor method belongs to the object method, starting with "-"
For example, the Init method is called when the object is created.
Person *p = [[Person alloc] init];
can be decomposed into
Person *P1 = [Person alloc]; Allocate space
Person *p = [P1 init]; Initializes the object, which is called the Init method
Writing format:
-(Instancetype) init
{
self = [super init];
if (self = nil) {//Here you can synthesize an if (self = [super init]) {
Initialize properties here}
}
return self;
}
Note:
(1) The parent class must be initialized before the subclass is initialized
(2) must determine if the parent class is initialized successfully, only the parent class initialization succeeds to continue initializing subclasses
(3) Returns the address of the current object
OC Foundation (vii) Construction method and custom construction method