- Hands-On Design Patterns with Swift
- Florent Vilmart Giordano Scalzo Sergio De Simone
- 135字
- 2021-07-02 14:45:10
Mutability and operations
These arrays are immutable, as they are defined as let; it is therefore not possible to add or remove elements from them. Hopefully, we can still make mutable copies of them:
var otherDoubles = doubles
otherDoubles.append(4)
let lastDoubles = otherDoubles.dropFirst()
print(doubles)
print(otherDoubles)
print(lastDoubles)
Take a minute to think about what will be printed in the console, and why:
[1.0, 2.0, 3.0]
[1.0, 2.0, 3.0, 4.0]
[2.0, 3.0, 4.0]
The first array, doubles, was never mutated, as we called append(4) on a copy of it. So the copy, otherDoubles, has the value 4.0 appended to the end. Finally, the dropFirst() call returns another copy of the array, and doesn't mutate in place. This is crucial to understand these, as this behavior is consistent across all Swift Standard Library value types.
推薦閱讀
- PyTorch深度學習實戰:從新手小白到數據科學家
- Microsoft SQL Server企業級平臺管理實踐
- Python金融大數據分析(第2版)
- Visual Studio 2015 Cookbook(Second Edition)
- Oracle RAC 11g實戰指南
- 云計算與大數據應用
- Sybase數據庫在UNIX、Windows上的實施和管理
- OracleDBA實戰攻略:運維管理、診斷優化、高可用與最佳實踐
- 大數據治理與安全:從理論到開源實踐
- Oracle數據庫管理、開發與實踐
- openGauss數據庫核心技術
- Practical Convolutional Neural Networks
- 深入理解Flink:實時大數據處理實踐
- 社交網站的數據挖掘與分析(原書第2版)
- 數據庫技術與應用:SQL Server 2008