8

I created a NSMutableArray in swift using let and when I add addObject in the mutableArray then it will add it even though I used the let to assign a constant. Can anyone explain how let works in swift? If it doesn't allow you to add value in later then how is the following code working?

let arr : NSMutableArray = [1,2,3,4,5]
arr.addObject(6)
println(arr)
5
  • let does not mean the array is not mutable. It means the variable arr cannot be set to any other object. Commented Jul 24, 2015 at 8:46
  • Can you please explain it with details? Because what I understand is let means a constant variable then how can we add an object to constant variable? Commented Jul 24, 2015 at 8:51
  • @ryancrunchi Actually the array is indeed not mutable when using let on it. Try it on a Swift array. Commented May 15, 2016 at 15:02
  • @Boon A NSMutableArray is mutable by definition... var or let, mutable is mutable. The difference is let cannot be reassigned to another object. But adding an object is not reassigning. That's why the code given works. Commented May 15, 2016 at 16:23
  • 1
    @ryancrunchi Understood, I am saying your statement only applies here because NSMutableArray is a reference type. In the case of value type, let actually will not allow you to modify the value, such is the case with Swift array. Commented May 15, 2016 at 21:40

2 Answers 2

10

Classes are reference types, and NSMutableArray is a class.

Foundation's NSMutableArray is different from Swift's Array: the latter is a value type.

If you create a constant NSMutableArray:

let ns: NSMutableArray = ["a", "b"]

then this works:

ns.addObject("c")

but this doesn't:

ns = ["d", "e"]   // nope!

because you can change the content of the reference but you can't change what is assigned to the constant.

On the other hand, with Swift's Array:

let sw: [String] = ["a", "b"]

the constant can't be changed because it's a value, not a reference.

sw.append("c")   // nope!

Doc: Structures and Enumerations Are Value Types and Classes Are Reference Types

Sign up to request clarification or add additional context in comments.

1 Comment

I believe this answer is the right answer: "Foundation's NSMutableArray is different from Swift's Array: the latter is a value type."
9

disclaimer: this answer only applies to NS type data structures, please see @Eric D's answer for the full picture

let when used with a class just means the variable cant be changed, eg, to another array. If you dont want the array to be editable, use a normal NSArray and not a mutable one

let arr : NSMutableArray = [1,2,3,4,5]
arr = [1,2,3,4,5] //error trying to assign to a let variable that has already been assigned

arr.addObject(6) //fine because we are not changing what is assigned to arr, but we are allowed to change the object that is assigned to arr itself

I think your understanding of what a constant variable is, is a bit too strict.

5 Comments

I wanted to know that if let means the variable that cannot changes then how can I change it using NSMuatbleArray?
let just means the value assigned to the variable cant change, not what ever is assigned to it cant change, for example you cant go, let arr = [1,2,3] then go arr = [4,5,6] cause thats assigning a completely different array to the variable
arr.addObject adds an object to the same variable, it does not assign a new array.
This is misleading - let doesn't just mean the variable cannot be changed. A regular Swift array cannot have its value changed when assigned to a let variable. Try arr[0] = 1 on it and see.
that is true @Boon, i wasnt thinking about the swift type array when answering, the other answer is correct

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.