Back to questions
JuniorTheoryCommon
What happens when this program is compiled and run?
struct Counter {
var count = 0
mutating func increment() {
count += 1
}
}
let c = Counter()
c.increment()
print(c.count)It does not compile
- Expecting
1: thinkingletonly stopscfrom being reassigned. An instance in aletcannot change at all, so the call to amutatingmethod does not compile. - Expecting
0: thinking Swift changes a hidden copy ofc. It makes no copy; calling amutatingmethod on aletis a compile error.
- What does the program print if
cis declared withvar? - Why does reading
c.countcompile on aletwhilec.increment()does not?