官术网_书友最值得收藏!

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.

主站蜘蛛池模板: 南阳市| 景泰县| 安西县| 汪清县| 达州市| 望都县| 贵溪市| 蒲江县| 威信县| 河曲县| 松溪县| 雷山县| 和田县| 广西| 洛浦县| 贡山| 康马县| 永昌县| 漾濞| 安岳县| 韶关市| 扎囊县| 东光县| 丹凤县| 津市市| 铜鼓县| 武威市| 溆浦县| 虹口区| 阜新| 湘乡市| 建昌县| 垣曲县| 共和县| 桃园县| 比如县| 石门县| 唐河县| 印江| 镶黄旗| 汾阳市|