官术网_书友最值得收藏!

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.

主站蜘蛛池模板: 新宁县| 铁岭市| 乌兰县| 成都市| 壤塘县| 闽清县| 皮山县| 岑溪市| 夏津县| 基隆市| 探索| 霍林郭勒市| 六盘水市| 加查县| 台山市| 滦平县| 武功县| 海林市| 南雄市| 栾川县| 基隆市| 大丰市| 婺源县| 绵阳市| 雷州市| 邵阳市| 宾阳县| 平利县| 云和县| 德州市| 公安县| 黑水县| 项城市| 吉安县| 长春市| 焦作市| 徐汇区| 抚宁县| 收藏| 布尔津县| 蓝田县|