ぜのぜ

しりとりしようぜのぜのぜのぜ

125日目

日記

相変わらずめんどくさがってご飯を炊いてないので今日もうどん.

今日書いたコード

Specifically, if a failable initializer delegates to an initializer that fails and returns nil, then the initializer that delegated also fails and implicitly returns nil. https://docs.swift.org/swift-book/ReferenceManual/Declarations.html#grammar_union-style-enum-case

class Super {
    var superProp: String

    init?(_ str: String) {
        if str.isEmpty {
            return nil
        }

        superProp = str
    }
}

class Sub: Super {
    var prop: String

    init?() {
        prop = "hoge"

        super.init("")
    }

    convenience init?(isHoge: Bool) {
        self.init()
    }
}

print(Sub()) // nil
print(Sub(isHoge: false)) // nil

感想

そういえばSwift イニシャライザ大全みたいなタイトルの本を読むって言っていたのを思い出した.