SoFunction
Updated on 2025-04-11

Swift study notes constructor reload

Like functions, methods also have overloading, and their overloading method is consistent with functions. So, as a special method of the constructor, does there also exist overloading? The answer is yes.

1. Constructor reload concept

The conditions for function overloading in Swift also apply to the constructor, as follows:
Functions have the same name;
The parameter list is different or the return value type is different, or the external parameter name is different;
The constructor in Swift can meet the following two conditions, the code is as follows:

Copy the codeThe code is as follows:

class Rectangle {
    var width : Double
    var height : Double
    init(width : Double, height : Double) {        ①
           = width
          = height
    }
    init(W width : Double,H height : Double) {        ②
           = width
          = height
    }
    init(length : Double) {        ③
           = length
          = length
    }
    init() {                                                ④
           = 640.0
          = 940.0
    }
}
var rectc1 = Rectangle(width : 320.0, height : 480.0)        ⑤
println("Rectangle:\() x \()")
var rectc2 = Rectangle(W : 320.0, H : 480.0)        ⑥
println("Rectangle:\() x \()")
var rectc3 = Rectangle(length: 500.0)        ⑦
println("Rectangle 3:\() x \()")
var rectc4 = Rectangle()        ⑧
println("Rectangle 4:\() x \()")

Lines ①~④ above define 4 constructors, the others are overloaded relationships. From the perspective of the number of parameters and parameter types, the constructors of line 1 and line 2 are the same, but their external parameter names are different, so the constructor of line 1 is called in line 5, and the constructor of line 2 is called in line 6.
The number of constructor parameters in line ③ and line ④ is different from that in line ①, so the constructor of line ③ is called in line ③, and the constructor of line ⑧ is called in line ⑧.

2. Value type constructor agent

In order to reduce code duplication between multiple constructors, when defining a constructor, some of the construction process of the instance can be completed by calling other constructors, which is called a constructor proxy. The constructor agent is used differently in value types and reference types. In this section, we will first introduce the value type constructor agent.
Modify the examples from the previous section as follows:

Copy the codeThe code is as follows:

struct Rectangle {
    var width : Double
    var height : Double
    init(width : Double, height : Double) {        ①
           = width
          = height
    }
    init(W width : Double,H height : Double) {        ②
           = width
          = height
    }
    init(length : Double) {        ③
        (W : length, H : length)
    }
    init() {                                                ④
        (width: 640.0, height: 940.0)
    }
}
var rectc1 = Rectangle(width : 320.0, height : 480.0)        ⑤
println("Rectangle:\() x \()")
var rectc2 = Rectangle(W : 320.0, H : 480.0)        ⑥
println("Rectangle:\() x \()")
var rectc3 = Rectangle(length: 500.0)        ⑦
println("Rectangle 3:\() x \()")
var rectc4 = Rectangle()        ⑧
println("Rectangle 4:\() x \()")

Declare Rectangle as a structure type, and there are also 4 constructor overloads. A statement is used in the constructors of line ③ and line ④. Self indicates the current instance itself. Init is its own constructor. The (W : length, H : length) statement in line ③ is calling the constructor defined by line ②, and the (width: 640.0, height: 940.0) statement in line ④ is calling the constructor defined by line ①.
This kind of calling through statements in the same type is what we call the constructor proxy.

3. Reference type constructor horizontal proxy

The reference type constructor proxy is a class constructor proxy. Because the class has an inheritance relationship, the class constructor proxy is relatively complex and is divided into horizontal proxy and upward proxy.
Horizontal proxy is similar to value type constructor proxy, which occurs within the same class. This constructor is called convenience initializers.
Upward proxy occurs in the case of inheritance. During the subclass construction process, the parent class constructor must be called first to initialize the storage properties of the parent class. This constructor is called a designated initializers.
Since we have not introduced inheritance yet, this chapter only introduces horizontal proxying.
Modify the examples from the previous section as follows:

Copy the codeThe code is as follows:

class Rectangle {
    var width : Double
    var height : Double
    init(width : Double, height : Double) {        ①
           = width
          = height
    }
    init(W width : Double,H height : Double) {        ②
           = width
          = height
    }
    convenience init(length : Double) {        ③
        (W : length, H : length)
    }
    convenience init() {        ④
        (width: 640.0, height: 940.0)
    }
}
var rectc1 = Rectangle(width : 320.0, height : 480.0)        ⑤
println("Rectangle:\() x \()")
var rectc2 = Rectangle(W : 320.0, H : 480.0)        ⑥
println("Rectangle:\() x \()")
var rectc3 = Rectangle(length: 500.0)        ⑦
println("Rectangle 3:\() x \()")
var rectc4 = Rectangle()        ⑧
println("Rectangle 4:\() x \()")

Declare Rectangle as a class, and there are 4 constructor overloads. Statements are used in the constructors in line ③ and line ④, and the convenience keyword is added before the constructor. Convenience means a convenience constructor, which shows that we define the constructor as a horizontal proxy to call other constructors.
The (W : length, H : length) statement in line 3 calls the constructor proxy defined in line 2 horizontally, and the (width: 640.0, height: 940.0) statement in line 4 calls the constructor proxy defined in line 1 horizontally.

This is the end of the problem of constructor reloading today. Friends can refer to the following examples, I hope it will be helpful to you.