Back to questions
JuniorTheoryCommon
What happens when this program is compiled and run?
let numbers = [1, 2, 3]
numbers.append(4)
print(numbers)It does not compile
- Expecting
[1, 2, 3, 4]: thinkingletfixes only the name. In Swift an array declared withletis a constant as a whole, soappenddoes not compile. - Expecting a crash on the
appendline: Swift checks changes to constants while compiling, before a single line runs.
- What does the program print if
numbersis declared withvar? - Which of
numbers + [4]andnumbers.append(4)works with aletarray, and why?