In the initial discussion I don’t understand why there need to be two initialisers. It isn’t clear from the text to me, all I see is a reference to “initialisers”. Why is there one init() with n parameters and that defines two parameters and assigns an x to itself as an x?
Hello
I think the the problem is :
Struct offers:
- a default initializer for their properties if all of them have a default value (for optional they receive the default nil) and there is not another custom initializer:
struct Pippo {
var x:Int = 3
var y:Int = 4
}
let pippo = Pippo() //init x with 3, the default
- a memberwise initializer if there aren’t custom initializers or default initializer due the fact that a property has not a default value
struct Pippo {
var x:Int = 3
let y:Int
}
let pipe = Pippo(x:3, y: 4) //the memberwise
- if there is a custom initializer the default and memberwise disappear, you don’t need them
struct Pippo {
var x:Int
let y:Int = 4
init(customX x:Int){ //customX is a alias for x
self.x = x // you need self because here the x is local and obscures the property of struct with same name
}
}
let pippo = Pippo(customX: 3) //this use the custom
So if you want a default-like initializer and a memberwise-like you have to write both of them