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

  • iOS Programming Cookbook
  • Hossam Ghareeb
  • 99字
  • 2021-07-09 18:29:41

Mutating methods

Swift allows you mark protocol methods as mutating when it's necessary for these methods to mutate (modify) the instance value itself. This is applicable only in structures and enumerations; we call them value types. Consider this example of using mutating:

protocol Togglable{ 
   mutating func toggle() 
} 
 
enum Switch: Togglable{ 
   case ON 
   case OFF 
    
   mutating func toggle() { 
       switch self { 
       case .ON: 
           self = OFF 
       default: 
           self = ON 
       } 
   } 
} 

The Switch enum implements the method toggle, as it's defined in the protocol Togglable. Inside toggle(), we could update self-value as function marked as mutating.

主站蜘蛛池模板: 德阳市| 宿州市| 岚皋县| 措美县| 虹口区| 区。| 昌黎县| 连平县| 贡山| 上犹县| 密山市| 富平县| 抚州市| 沈阳市| 宜章县| 呼伦贝尔市| 林甸县| 灌阳县| 永安市| 萨嘎县| 蒙阴县| 五指山市| 蒙自县| 龙陵县| 韶山市| 神池县| 龙里县| 梨树县| 阳原县| 嵩明县| 怀安县| 星子县| 新营市| 淄博市| 延吉市| 兰州市| 二连浩特市| 陆丰市| 岳阳县| 正宁县| 红桥区|