swift3.0枚举遵守协议

这是swift3.0官方文档协议那一节给出的协议

protocol ExampleProtocol {
   var simpleDescription: String { get }
   func adjust()
}

所留练习: 写一个实现这个协议的枚举 用的很多方法 都没实现(本人悟性比较低), 最后在网上找了相关文章 终于实现了 记录下来以便以后查看

enum SimpleEnum: ExampleProtocol {
    case Result(String)
    case Failure(String)

    var simpleDescription: String{
        get {
            switch self {
            case .Result("123"):
                return "13"
            case .Failure("zhang"):
                return "zhang"
            default:
                return "get error"
            }
        }
        set {
            switch self {
            case .Result("123"):
                self = .Result("12")
            case .Failure("zhang"):
                self = .Failure("zhang")
            default:
                self = .Failure("1333")
            }
        }
    }
//    case sime
    mutating func adjust() {
        switch self {
        case let .Result(text):
            self = .Result(text + " (first case adjusted)")
        case let .Failure(text):
            self = .Failure(text + " (second case adjusted)")
        }
    }
}
var enumConformToProtocolTest = SimpleEnum.Failure("zhang")
//enumConformToProtocolTest.simpleDescription
enumConformToProtocolTest.adjust()
enumConformToProtocolTest.simpleDescription
print(enumConformToProtocolTest)

enumConformToProtocolTest = SimpleEnum.Result("ThirdVal")
enumConformToProtocolTest.simpleDescription
enumConformToProtocolTest.adjust()
enumConformToProtocolTest.simpleDescription

你可能感兴趣的:(swift3.0枚举遵守协议)