Articles, podcasts and news about Swift development, by John Sundell.

Assigning to self in struct initializers

Published on 11 Aug 2017
Basics article available: Protocols

It's so nice that you can assign directly to self in struct initializers in Swift. Very useful when adding conformance to protocols.

extension Bool: AnswerConvertible {
    public init(input: String) throws {
        switch input.lowercased() {
        case "y", "yes", "👍":
            self = true
        default:
            self = false
        }
    }
}