How to make an enum conform to a protocol in Swift?

前端 未结 15 1306
终归单人心
终归单人心 2020-12-22 16:32

Swift documentation says that classes, structs, and enums can all conform to protocols, and I can get to a point where they all conform. But I can

15条回答
  •  醉话见心
    2020-12-22 17:13

    how about this

    enum SimpleEnum : ExampleProtocol {
        case Desc(String)
        init() {
            self = Desc("a simple enum")
        }
        var simpleDescription:String {
            get {
                return (Mirror(reflecting: self).children.first!.value as? String)!
            }
        }
        mutating func adjust() {
            self = SimpleEnum.Desc(self.desc + " adjusted")
        }
    }
    var e = SimpleEnum()
    e.simpleDescription    # => "a simple enum"
    e.adjust()
    e.simpleDescription    # => "a simple enum adjusted"
    

提交回复
热议问题