In Swift, the let
keyword denotes immutability. What does it mean to the compiler why you combine let
and NSMutable
?
e.g.
let nsArray: NSMutableArray = ["a", "b"];
nsArray.addObject("c") // Still works
In Swift, the let
keyword denotes immutability. What does it mean to the compiler why you combine let
and NSMutable
?
e.g.
let nsArray: NSMutableArray = ["a", "b"];
nsArray.addObject("c") // Still works
NSMutableArray
is a class so it's passed by reference, not by value: here what is constant is your nsArray
object, not the mutable array it contains.
So you can do:
nsArray.addObject("c")
But you can't do:
nsArray = ["d", "e"]
Interesting question. This is because you're not assigning the array itself. If you instead did:
nsArray = ["a", "sb"]
Then you would get a compiler error. This is somewhat related to the discussion here: let Non-mutable array in swift