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

前端 未结 15 1289
终归单人心
终归单人心 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:07

    I was thinking that the goal is simply to retain state and use a description to make the current state easier to read:

    enum SimpleEnum: ExampleProtocol {
    
        case Default, Adjusted
    
        init() {
            self = .Default
        }
    
        var simpleDescription: String { get { return "\(self) Value" }}
    
        mutating func adjust() {
            self = .Adjusted
        }
    }
    
    var simpleEnum = SimpleEnum()
    simpleEnum.adjust()
    let adjustedSimple = simpleEnum.simpleDescript
    

提交回复
热议问题