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

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.

主站蜘蛛池模板: 柘荣县| 勐海县| 江门市| 绿春县| 衡东县| 海门市| 西城区| 汉阴县| 嘉禾县| 荔浦县| 京山县| 鄂伦春自治旗| 江源县| 绥德县| 分宜县| 隆尧县| 宁都县| 三台县| 白银市| 佛冈县| 鄂托克前旗| 澳门| 梨树县| 西贡区| 修水县| 都昌县| 岱山县| 郴州市| 莱芜市| 青神县| 西贡区| 三门峡市| 内丘县| 互助| 滕州市| 龙泉市| 津南区| 东城区| 米林县| 赫章县| 华阴市|