- Hands-On Design Patterns with Swift
- Florent Vilmart Giordano Scalzo Sergio De Simone
- 89字
- 2021-07-02 14:44:58
Adding methods
Now, we may want to add a method to this State enumeration. After all, it's very common to just toggle the switch on and off without thinking:
extension State {
mutating func toggle() {
self = self == .off ? .on : .off
}
}
var state: State = .on
state.toggle()
state == .off // true
As in the previous section, we can just extend the State enum to add the toggle functionality. Enums follow value semantics; therefore, we have to mark the toggle method as mutating.
推薦閱讀
- 企業(yè)數(shù)字化創(chuàng)新引擎:企業(yè)級(jí)PaaS平臺(tái)HZERO
- 數(shù)據(jù)分析實(shí)戰(zhàn):基于EXCEL和SPSS系列工具的實(shí)踐
- SQL Server 2016 數(shù)據(jù)庫(kù)教程(第4版)
- SQL Server 2012數(shù)據(jù)庫(kù)技術(shù)與應(yīng)用(微課版)
- Python金融大數(shù)據(jù)分析(第2版)
- 大數(shù)據(jù)可視化
- 文本數(shù)據(jù)挖掘:基于R語(yǔ)言
- MySQL從入門到精通(第3版)
- Mockito Cookbook
- Hands-On Mathematics for Deep Learning
- “互聯(lián)網(wǎng)+”時(shí)代立體化計(jì)算機(jī)組
- R Object-oriented Programming
- MySQL數(shù)據(jù)庫(kù)實(shí)用教程
- Node.js High Performance
- 區(qū)塊鏈應(yīng)用開發(fā)指南:業(yè)務(wù)場(chǎng)景剖析與實(shí)戰(zhàn)