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

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

    My first contribution here:

    enum SimpleEnum: ExampleProtocol {
        case Basic(String), Adjusted(String)
        init() {
            self = SimpleEnum.Basic("A simple Enum")
    
        }
    
        var simpleDescription: String {
            get {
                switch self {
                case let .Basic(string):
                    return string
                case let .Adjusted(string):
                    return string
                }
            }
        }
    
        mutating func adjust() {
            self = SimpleEnum.Adjusted("full adjusted")
    
        }
    }
    
    var c = SimpleEnum()
    c.adjust()
    let cDescription = c.simpleDescription
    

    Thanks for others!

提交回复
热议问题