- Reactive Programming with Swift 4
- Navdeep Singh
- 172字
- 2021-06-24 18:58:00
swap versus swapAt
The swap(_:_: ) method in Swift 3 works on "pass by reference principle" and swaps two elements of a given array on the spot. In pass by reference, actual memory addresses are used rather than values:
var integerArray = [1, 2, 4, 3, 5]
swap(integerArray [2], integerArray [3])
As you can see, the parameters are passed as in out parameters, which means the actual references or placeholder addresses are accessible directly inside the function. On the other hand, Swift 4’s swapAt(_:_:) works on “pass by value” principle and only the corresponding indices are passed to the function to be swapped:
integerArray.swapAt(2, 3)
The swap(_:_:) function will not be seen in Swift 4, because it will be deprecated and removed, and you have a couple of approaches to replace it. The first approach uses a temporary constant as follows:
let temp = a
a = b
b = temp
The second takes advantage of Swift's built in tuples, as follows:
(b, a) = (a, b)
- Unreal Engine Physics Essentials
- Python數(shù)據(jù)分析入門(mén)與實(shí)戰(zhàn)
- 劍指JVM:虛擬機(jī)實(shí)踐與性能調(diào)優(yōu)
- Mastering Natural Language Processing with Python
- SQL基礎(chǔ)教程(視頻教學(xué)版)
- C程序設(shè)計(jì)案例教程
- Drupal 8 Module Development
- 學(xué)習(xí)正則表達(dá)式
- Kubernetes進(jìn)階實(shí)戰(zhàn)
- Mastering OAuth 2.0
- Software-Defined Networking with OpenFlow(Second Edition)
- Visual Basic語(yǔ)言程序設(shè)計(jì)上機(jī)指導(dǎo)與練習(xí)(第3版)
- Splunk Essentials
- Building Microservices with Go
- 趣學(xué)數(shù)據(jù)結(jié)構(gòu)