- 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)
- 精通JavaScript+jQuery:100%動態網頁設計密碼
- LabVIEW 2018 虛擬儀器程序設計
- Django:Web Development with Python
- Ray分布式機器學習:利用Ray進行大模型的數據處理、訓練、推理和部署
- 精通網絡視頻核心開發技術
- Kali Linux Wireless Penetration Testing Beginner's Guide(Third Edition)
- 全棧自動化測試實戰:基于TestNG、HttpClient、Selenium和Appium
- Learning Laravel's Eloquent
- Linux C編程:一站式學習
- Scala Data Analysis Cookbook
- 區塊鏈項目開發指南
- Scratch從入門到精通
- iOS開發項目化入門教程
- Drupal 8 Development:Beginner's Guide(Second Edition)
- Hands-On Dependency Injection in Go