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

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.

主站蜘蛛池模板: 松潘县| 大宁县| 五指山市| 岑溪市| 三河市| 静海县| 南部县| 广东省| 渑池县| 平谷区| 肇庆市| 石楼县| 德保县| 乌兰察布市| 故城县| 博湖县| 张北县| 徐闻县| 广饶县| 申扎县| 甘孜县| 嘉禾县| 兴安县| 楚雄市| 靖安县| 淳化县| 游戏| 抚顺市| 铅山县| 金溪县| 佳木斯市| 邹平县| 临湘市| 招远市| 龙海市| 凌云县| 昭平县| 屯昌县| 日照市| 安徽省| 海门市|