First, Swift defines two types of initializers for the class to ensure that all the stored properties in the class can get an initialization value. The two types of initializers are "specify initializers" (designated Initializer) and "convenience initializers" (convenience Initializer).
Specifying initializers and convenience initializers
Specifies that the initializer is the primary initializer for the class, initializes all the property assignments in the class, and invokes the specified initializer of the class's parent class all the way up to initialize the attributes that they have introduced. A class can have many specified initializers, or only one, but must have at least one.
The convenience initializer, as its name implies, is a developer-friendly initializer that must invoke the specified initializer of the same class, providing some default parameters to the specified initializer to generate an instance of the default class. A class can have no convenience initializer.
Initializer chain
To simplify the relationship between the specified initializer and the convenience initializer, we have set out the following three rules:
Specifies that the initializer must call the specified initializer of its parent class
The convenience initializer initializer must call another initializer in the class
The convenience initializer must eventually invoke a specified initializer
To put it simply, the specified initializer must be called up in the class's inheritance relationship, and a convenient initializer must go all the way to the specified initializer in the class.
To draw a diagram, that is:
As you can see, the parent class has a specified initializer and two convenient initializers, and one convenient initializer invokes another traversal initializer, but eventually they will invoke the specified initializer.
In the subclass, however, specifies that the initializer is ultimately up, calling the specified initializer of its parent class.
Two steps to initialize
In Swift, class initialization takes two steps, and in the first step each of the stored properties introduced by the class needs to be assigned an initial value. When you are done, perform the second step, and each class can then make custom modifications to the properties.
In detail, that is:
Step One
The specified or facilitated initializer for a class needs to be called
The memory of the instance of the class is requested but has not yet been initialized
Specifies that the initializer ensures that all storage properties introduced by the current class are initialized
Specifies that the initializer invokes the specified initializer of its parent class, and continues to let the parent class initialize the stored properties that it introduces
The above process continues until the specified initializer for the final root class is called
When the specified initializer for the root class is called, an instance of the class has ensured that all the stored properties have been initialized and completed.
Step Two
Step two is starting from the specified initializer of the root class, one layer to the final subclass of the specified initializer
In this step, each class has the opportunity to make a custom modification to the instance that has already been initialized in step one. At this point the initializer can already access self, modify properties, or invoke instance methods.
initializer inheritance and overrides
Classes that are not like objective-c,swift do not inherit the initializer of their parent class by default. There are exceptions, however, that subclasses automatically inherit the initializer of the parent class as long as certain conditions are met. We define the following rules:
Rule One
If your subclass does not define any of the specified initializers, it automatically inherits all the specified initializers of its parent class
Rule Two
If your subclass implements all the specified initializers for its parent class, whether it is a custom implementation or because of the implementation of rule one, the subclass automatically inherits all the handy initializers of its parent class.
Interacting with the objective-c initializer
Swift calls the class of objective-c, whose initialization method automatically omits the "Init" or "initwith" keyword. Like what
1234 |
//Objective-C UILabel *label = [UILabel alloc] init]; UITableView *myTableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped]; |
In Swift, write
1234 |
//Swift let label: UILabel = UILabel() let myTableView: UITableView = UITableView(frame: CGRectZero, style: .Grouped) |
You don't need to call Alloc,swift to get this done.
In addition, some of the convenient factory methods for class initialization in some objective-c are also mapped to Swift, such as
12 |
//Objective-C UIColor *color = [UIColor colorWithRed:0.5 green:0.0 blue:0.5 alpha:1.0]; |
In Swift:
12 |
//Swift let color = UIColor(red: 0.5, green: 0.0, blue: 0.5, alpha: 1.0) |
Initialization and inheritance of classes in Swift