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

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.

主站蜘蛛池模板: 张家界市| 嘉义县| 德庆县| 玉门市| 彭阳县| 雅安市| 如皋市| 习水县| 金溪县| 曲周县| 腾冲县| 垦利县| 桦川县| 台东市| 洛阳市| 巩义市| 朝阳区| 阳原县| 昭苏县| 晋江市| 米泉市| 霍州市| 云安县| 榆林市| 宁夏| 江北区| 霍林郭勒市| 桂平市| 盐源县| 巫山县| 萨嘎县| 云林县| 梓潼县| 斗六市| 颍上县| 清新县| 宜川县| 陵川县| 滁州市| 内乡县| 铜川市|