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

  • iOS Programming Cookbook
  • Hossam Ghareeb
  • 120字
  • 2021-07-09 18:29:43

Mutating instance methods

When you add instance methods, you can let them mutate (modify) the instance itself. In methods we've added before, we just do some logic and return a new value, and the instance value remains the same. With mutating, the value of instance itself will be changed. To do so, you have to mark your instance method with the mutating keyword. Let's take a look at an example:

extension Int{ 
   mutating func square(){ 
       self = self * self 
   } 
    
   mutating func double(){ 
       self = self * 2 
   } 
} 
 
var value = 8 
value.double() // 16 
value.square() // 256 

When you mark your method as mutating, it lets you to change self and assign new value to it.

主站蜘蛛池模板: 达孜县| 鄂尔多斯市| 玉树县| 舞钢市| 晴隆县| 灌南县| 哈巴河县| 桂林市| 石景山区| 汤原县| 兴文县| 永清县| 济宁市| 思茅市| 托克托县| 惠东县| 陇西县| 和林格尔县| 郸城县| 伽师县| 新巴尔虎右旗| 长寿区| 昆明市| 屯门区| 依兰县| 茌平县| 湖南省| 濮阳县| 盱眙县| 阳泉市| 沙田区| 增城市| 霍邱县| 定州市| 千阳县| 桑植县| 翁牛特旗| 阳山县| 松江区| 启东市| 名山县|