- 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.
推薦閱讀
- 數(shù)據(jù)產(chǎn)品經(jīng)理高效學(xué)習(xí)手冊(cè):產(chǎn)品設(shè)計(jì)、技術(shù)常識(shí)與機(jī)器學(xué)習(xí)
- 企業(yè)大數(shù)據(jù)系統(tǒng)構(gòu)建實(shí)戰(zhàn):技術(shù)、架構(gòu)、實(shí)施與應(yīng)用
- 文本挖掘:基于R語(yǔ)言的整潔工具
- Oracle高性能自動(dòng)化運(yùn)維
- 智能數(shù)據(jù)分析:入門(mén)、實(shí)戰(zhàn)與平臺(tái)構(gòu)建
- “互聯(lián)網(wǎng)+”時(shí)代立體化計(jì)算機(jī)組
- SQL優(yōu)化最佳實(shí)踐:構(gòu)建高效率Oracle數(shù)據(jù)庫(kù)的方法與技巧
- 中文版Access 2007實(shí)例與操作
- 數(shù)據(jù)挖掘與機(jī)器學(xué)習(xí)-WEKA應(yīng)用技術(shù)與實(shí)踐(第二版)
- 數(shù)據(jù)中臺(tái)實(shí)戰(zhàn):手把手教你搭建數(shù)據(jù)中臺(tái)
- 領(lǐng)域驅(qū)動(dòng)設(shè)計(jì)精粹
- 云原生架構(gòu):從技術(shù)演進(jìn)到最佳實(shí)踐
- SQL Server 2012 數(shù)據(jù)庫(kù)教程(第3版)
- 數(shù)據(jù)庫(kù)技術(shù)與應(yīng)用:SQL Server 2008
- 大數(shù)據(jù)用戶行為畫(huà)像分析實(shí)操指南