Swift Value Semantics & COW

Prefer value semantics for predictability.

Swift collections use Copy-on-Write (COW) to keep copies cheap until mutation.


Syntax

var a = [1,2,3]

Arrays, sets, and dictionaries use value semantics.


Copy-on-Write

Swift collections use Copy-on-Write (COW) to keep copies cheap until mutation.

Example

var a = [1,2,3]
var b = a // shares storage
b.append(4) // triggers copy for b only
print(a) // [1,2,3]
print(b) // [1,2,3,4]

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *