Nilai default bidang struct Swift
Swift 5.1:
Variables, marked with var are optional in constructors and their default value will be used. Example:
struct Struct {
var param1: Int = 0
let param2: Bool
}
let s1 = Struct(param2: false) // param1 = 0, param2 = false
let s2 = Struct(param1: 1, param2: true) // param1 = 1, param2 = true
For older versions of Swift (< 5.1):
Default Initialization is all-or nothing for a Struct. Either you define defaults for all properties and you get an automatically generated initializer Struct1() plus an initializer with all of the properties, or you get only the initializer with all of the properties, including any that happen to have a set default value. You're going to have to implement a custom initializer.
struct Struct1 {
let myLet = "my let"
let myLet2: Bool
let myLet3: String
init(myLet2: Bool, myLet3: String) {
self.myLet2 = myLet2
self.myLet3 = myLet3
}
}
Mobile Star