Declaring conformance to @objc protocol in empty extension breaks with EXC_BAD_INSTRUCTION

后端 未结 1 1393
旧巷少年郎
旧巷少年郎 2021-01-23 11:33

Been having lots and lots of trouble with Swift protocols in combination with arrays, but I couldn\'t even reproduce my whole problem before things started to break in playgroun

1条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-01-23 12:13

    I think that the problem is about the Utterable protocol having a property, which is already implemented in the concrete class.

    As you probably know, an extension cannot define stored properties (computed only). By adopting the protocol in the extension, something wrong happens - and it is clearly a bug (it should just work or the compiler should raise a compilation error).

    To fix it, just adopt the protocol in the class declaration rather than to the extension:

    class Bus : Displayable, Utterable { var name = "a bus"; var utterance = "this is a bus"}
    
    extension Bus  {}
    

    Surprisingly, turning the utterance property into a computed one and moving it in the extension body:

    extension Bus : Utterable {
        var utterance: String { return "this is a bus" }
    }
    

    doesn't solve the problem - still the same error. I consider it as a prove that it's a bug.

    0 讨论(0)
提交回复
热议问题