Value Semantics
Array Copy
Swift arrays behave like values when a program updates a copied variable.
Append to one array
array_copy.swift
let extra =
let first = [1, 2, 3]
var second = first
second.append(extra)
let message = "first=\(first.count), second=\(second.count), last=\(second.last!)"
print(message)
let extra =
let first = [1, 2, 3]
var second = first
second.append(extra)
let message = "first=\(first.count), second=\(second.count), last=\(second.last!)"
print(message)
let extra =
let first = [1, 2, 3]
var second = first
second.append(extra)
let message = "first=\(first.count), second=\(second.count), last=\(second.last!)"
print(message)
array value
Updating a copied array variable does not change the earlier array variable.