- 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.
推薦閱讀
- Greenplum:從大數(shù)據(jù)戰(zhàn)略到實(shí)現(xiàn)
- SQL Server 2012數(shù)據(jù)庫技術(shù)與應(yīng)用(微課版)
- 數(shù)據(jù)之巔:數(shù)據(jù)的本質(zhì)與未來
- Libgdx Cross/platform Game Development Cookbook
- 大數(shù)據(jù)算法
- Instant Autodesk AutoCAD 2014 Customization with .NET
- Oracle數(shù)據(jù)庫管理、開發(fā)與實(shí)踐
- 智慧城市中的大數(shù)據(jù)分析技術(shù)
- Scratch 2.0 Game Development HOTSHOT
- MySQL數(shù)據(jù)庫應(yīng)用與管理
- Microsoft Dynamics NAV 2015 Professional Reporting
- 大數(shù)據(jù)測試技術(shù):數(shù)據(jù)采集、分析與測試實(shí)踐(在線實(shí)驗(yàn)+在線自測)
- Machine Learning for Mobile
- 數(shù)據(jù)庫原理及應(yīng)用實(shí)踐教程
- Python數(shù)據(jù)分析入門與實(shí)戰(zhàn)