SoFunction
Updated on 2025-04-11

Analysis of initialization examples of classes and structures in Swift

Preface: Through learning and studying the official documentation of swift3.0, this part about initialization can be summarized into one sentence: Classes and structures are to ensure that an instance of a type can be used before all its storage properties are assigned correctly.

1. Initialization of the structure

struct FirstStruct{
     let b:Int
     let c:Int
     init() {
        b = 2
        c = 3
     }
 }
 let fs = FirstStruct()

Analysis: In this structure, the two storage properties b and c do not assign initial values, but assign initial values ​​in the init initializer (equivalent to the constructor in Java).

2. Initialization of structure

 struct SecondStruct{
     let b:Int
     let c:Int
 }
 let ss = SecondStruct(b:2,c:3)

Analysis: In the structure, the compiler will automatically generate a memberwise initializer, so the initialization value must be passed in when calling the structure.

3. Example of initialization error in structure

//struct ThirdStruct{
//    let b:Int
//    let c:Int
//    init() {
//        b = 0
//    }
//}
//let ts = ThirdStruct(c:3)

Note: The initializer is declared in the structure, and the compiler will not add a memberwise initializer to us. (The principle is consistent with the empty constructor of Java) The above example cannot be compiled.

4. In the structure, init can call other inits through the self keyword.

struct FourthStruct{
     let b:Int
     let c:Int
     init() {
         (b:2)
     }
     init(b:Int){
         c = 2
          = b
     }
 }
 let fos = FourthStruct()

Five, Class Initialization Error Example

 //Error Example //class SecondClass{
 //    let b:Int
 //    let c:Int
 //}
 //let sc = SecondClass(b:2,c:3)

Analysis: Init initializer can also be declared in the class. If the initializer is not provided, the compiler will not provide the default initializer.

Six, Class (Designated init) Initializer

class Parent2{
     var name:String
     var height:Int
     init(name:String,height:Int){
          = name
          = height
     }
 }
 let p2 = Parent2(name:"zs",height:168)
 
 

Analysis: Init without special modifiers is a special (Designated init) initializer, and its main function is to ensure that all storage attributes are initialized.

7. Designated init

Generally, there are only one, and there can be multiple. Different initializers are called according to the assignment when you call.

class Parent3{
     var name:String
     var sex:String
     init(name:String,sex:String){
          = name
          = sex
     }
     init(name:String){
          = name
         sex = "male"
     }
 }
 let p3 = Parent3(name:"zs",sex:"female")
 let p4 = Parent3(name:"ls")
 
 

8. Class convenience initializer

class Parent3{
    var name:String
    var sex:String
    init(name:String,sex:String){
         = name
         = sex
    }
    convenience init(name:String){
        (name:name,sex:"male")
    }
    convenience init(){
        (name:"zs")
    }
}
let p3 = Parent3(name:"zs")
let p4 = Parent3()

Analysis: By declaring the convenience keyword before the initializer, a convenient initializer can be declared. Its purpose is to facilitate use when creating an object, but it must directly or indirectly call the special initializer.

Nine, a special initializer for subclasses

The special initializer of a subclass must call the special initializer of the parent class, and the subclass cannot call the convenient initializer of the parent class.

class Parent4{
     var name:String
     var sex:String
     init(name:String,sex:String){
          = name
          = sex
     }
     convenience init(name:String){
         (name:name,sex:"male")
     }
 }
 //-----------------------------------------------
 class Child4:Parent4{
     var age:Int
     init(age:Int) {
          = age
         //(name: "ls") Error example         (name: "zs",sex:"Demon")
     }
    convenience init(){
     (age:12)
     }
 }
 let c4 = Child4()
 

The above is the detailed content of the initialization example analysis of classes and structures in Swift. For more information about Swift class and structure initialization, please pay attention to my other related articles!