Value Semantics

suggest change

Copying an array copies its elements. For String values, changing the new array will not change the original array. If an element is a class instance, both arrays still refer to that same instance.

var originalArray = ["Swift", "is", "great!"]
var newArray = originalArray
newArray[2] = "awesome!"
//originalArray = ["Swift", "is", "great!"]
//newArray = ["Swift", "is", "awesome!"]

Copied arrays share storage with the original until one of them changes. The first change copies that storage, so it costs more than later changes to the same array.

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents