Hands-On Design Patterns with Swift
上QQ阅读APP看书,第一时间看更新

Conformance at declaration

The first method to declare a conformance is to do it at the top level, when you declare your custom type. You'll notice that the raw representation comes first, then the protocol conformance:

enum State: Int, Toggling {
case off = 0
case on

mutating func toggle() {
self = self == .on ? .off : .on
}
}

var state: State = .on
state.toggle()
assert(state == .off)