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

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.

主站蜘蛛池模板: 泊头市| 棋牌| 平乡县| 奉贤区| 依兰县| 阜新市| 三江| 石棉县| 芜湖县| 阿拉善右旗| 铜鼓县| 鞍山市| 乌苏市| 双桥区| 临海市| 新龙县| 南京市| 汤阴县| 崇信县| 余姚市| 高雄县| 唐山市| 齐河县| 锦屏县| 越西县| 叶城县| 宁夏| 临颍县| 稻城县| 建水县| 景洪市| 南充市| 苍溪县| 长沙市| 阳山县| 宜宾市| 滕州市| 舞钢市| 平武县| 肇源县| 郑州市|