- 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.
推薦閱讀
- 30天自制操作系統(tǒng)
- Implementing Cisco UCS Solutions
- Linux內(nèi)核完全注釋(20周年版·第2版)
- Learning Android Intents
- Extending Puppet
- Windows Phone 7.5 Data Cookbook
- 開源安全運維平臺OSSIM疑難解析:入門篇
- 數(shù)據(jù)中心系統(tǒng)工程及應用
- 無蘋果不生活 The New iPad隨身寶典
- iOS 8開發(fā)指南
- Red Hat Enterprise Linux 6.4網(wǎng)絡(luò)操作系統(tǒng)詳解
- Linux系統(tǒng)最佳實踐工具:命令行技術(shù)
- Windows 7實戰(zhàn)從入門到精通(超值版)
- Linux網(wǎng)絡(luò)配置與安全管理
- 大規(guī)模分布式系統(tǒng)架構(gòu)與設(shè)計實戰(zhàn)