Back to questions
JuniorTheoryCommon
What does the program print?
let start = [5, 6, 7]
var list = start
list.append(8)
print(start.count, list.count)3 4
- Expecting
4 4: thinkingvar list = startgives a second name to the same array. An array is a value type, solistis a separate copy. - Expecting a compile error: thinking a copy of a
letarray stays constant. The copy follows its own declaration, andlistis avar.
- When does Swift actually copy the elements of
start? - How can a function add an element to the caller's array?