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

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.

主站蜘蛛池模板: 新沂市| 米泉市| 芷江| 涿州市| 和硕县| 龙岩市| 错那县| 星座| 太原市| 丹江口市| 酒泉市| 永修县| 遂川县| 古蔺县| 清镇市| 若羌县| 合阳县| 威信县| 河源市| 封开县| 从化市| 泗水县| 房产| 兰州市| 射洪县| 英山县| 和平县| 开封市| 铁岭市| 卢湾区| 本溪| 茂名市| 黑河市| 太保市| 镇巴县| 泰来县| 德令哈市| 巨鹿县| 德州市| 青河县| 连州市|