上QQ阅读APP看书,第一时间看更新
Mutating methods
Swift allows you mark protocol methods as mutating when it's necessary for these methods to mutate (modify) the instance value itself. This is applicable only in structures and enumerations; we call them value types. Consider this example of using mutating:
protocol Togglable{ mutating func toggle() } enum Switch: Togglable{ case ON case OFF mutating func toggle() { switch self { case .ON: self = OFF default: self = ON } } }
The Switch enum implements the method toggle, as it's defined in the protocol Togglable. Inside toggle(), we could update self-value as function marked as mutating.