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

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.

主站蜘蛛池模板: 都兰县| 永新县| 永康市| 县级市| 衡阳县| 桦甸市| 镇远县| 赣榆县| 循化| 寿光市| 古浪县| 牟定县| 慈利县| 肥西县| 清水河县| 尼木县| 广州市| 南宫市| 岑溪市| 公主岭市| 乌什县| 石首市| 隆德县| 舞钢市| 通化市| 莲花县| 兴文县| 化德县| 利辛县| 阳江市| 凉山| 芒康县| 湟中县| 柳林县| 莆田市| 普宁市| 汉阴县| 彩票| 朝阳区| 湟源县| 从江县|