- 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)
- Deploying Node.js
- ASP.NET Core 5.0開發入門與實戰
- Flash CS6中文版應用教程(第三版)
- Interactive Applications Using Matplotlib
- Visual Basic程序設計
- 從零開始學Android開發
- 從零開始學UI:概念解析、實戰提高、突破規則
- Practical Maya Programming with Python
- Web開發的平民英雄:PHP+MySQL
- 實驗編程:PsychoPy從入門到精通
- Node.js實戰:分布式系統中的后端服務開發
- 從零開始學算法:基于Python
- 精益軟件開發管理之道
- Web開發新體驗
- Vue.js從入門到精通