+ 2

why the value still can be changed

class C{ var a = 5 } let c = C() c.a = 6 c.a = 7 print(c.a) // why the value still can be changed even though we use let

1st Aug 2020, 11:13 AM
Zhengrong Yan
Zhengrong Yan - avatar
2 Respostas
+ 1
You don't change the value of your var c, just the value of one of its attributes. That's a big difference. c is still the same object and remains, even if you modify the value of any attribute. OOP basics.
1st Aug 2020, 1:31 PM
Sandra Meyer
Sandra Meyer - avatar
+ 1
PS: easy cross-check for you, try this: let c = C() // valid c.a = 0 // valid c = C() // failure
1st Aug 2020, 1:33 PM
Sandra Meyer
Sandra Meyer - avatar