Swift arrays behave like values when a program updates a copied variable.

Append to one array

extra
array_copy.swift
Replay: real traced execution (multi-file project)
let extra = 5
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 = 2
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 = 9
let first = [1, 2, 3]
var second = first
second.append(extra)
let message = "first=\(first.count), second=\(second.count), last=\(second.last!)"

print(message)
  1. extra ← 5, first ← [1, 2, 3], second ← [1, 2, 3], message ← first=3, second=4, last=5

    1let extra→ 5 = 5  //@extra=2, 92let first→ [1, 2, 3] = [1, 2, 3]3var second→ [1, 2, 3] = first[1, 2, 3]4second→ [1, 2, 3, 5].append(extra5)5let message→ first=3, second=4, last=5 = "first=\(first.count3), second=\(second.count4), last=\(second.lastOptional(5)!)"67print(messagefirst=3, second=4, last=5)
    outputfirst=3, second=4, last=5
  1. extra ← 2, first ← [1, 2, 3], second ← [1, 2, 3], message ← first=3, second=4, last=2

    1let extra→ 2 = 22let first→ [1, 2, 3] = [1, 2, 3]3var second→ [1, 2, 3] = first[1, 2, 3]4second→ [1, 2, 3, 2].append(extra2)5let message→ first=3, second=4, last=2 = "first=\(first.count3), second=\(second.count4), last=\(second.lastOptional(2)!)"67print(messagefirst=3, second=4, last=2)
    outputfirst=3, second=4, last=2
  1. extra ← 9, first ← [1, 2, 3], second ← [1, 2, 3], message ← first=3, second=4, last=9

    1let extra→ 9 = 92let first→ [1, 2, 3] = [1, 2, 3]3var second→ [1, 2, 3] = first[1, 2, 3]4second→ [1, 2, 3, 9].append(extra9)5let message→ first=3, second=4, last=9 = "first=\(first.count3), second=\(second.count4), last=\(second.lastOptional(9)!)"67print(messagefirst=3, second=4, last=9)
    outputfirst=3, second=4, last=9
array value Updating a copied array variable does not change the earlier array variable.