Tsallaka zuwa abun ciki
Justice Tech School

Initialisers

Designated initialisers set every property. Convenience and failable ones add options.

Example
struct Temp {
    var c: Double
    init?(kelvin: Double) { if kelvin < 0 { return nil }; c = kelvin - 273.15 }
}
print(Temp(kelvin: 300)!.c)
print(Temp(kelvin: -1) == nil)

Exercise

Failable init.

init(k: Double)

Quiz Test

What does a failable init return?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.