How do I create an immutable array in Swift?
A superficial reading of the docs would suggest you can just do
let myArray = [1,2,3]
It is now possible.
From Apple Developer
If you assign an array or a dictionary to a constant, that array or dictionary is immutable, and its size and contents cannot be changed.
So now
let myArray = [1,2,3]
produces completely immutable array. Yay!
There is not a great answer for this, and it is bizarre.
You can, however, prevent accidental mutation of arrays as they flow through your program by calling yourArray.unshare()
. This causes the array to be copied when it's assigned to a new variable.
This has changed with Xcode 6 beta 3. While arrays used to be semi-mutable, as you describe, with their elements changeable but their length fixed, now immutable arrays share the same value semantics as Dictionaries:
From the Xcode 6 beta 3 release notes:
• Array in Swift has been completely redesigned to have full value semantics like Dictionary and String have always had in Swift. This resolves various mutability problems – now a 'let' array is completely immutable, and a 'var' array is completely mutable – composes properly with Dictionary and String, and solves other deeper problems. Value semantics may be surprising if you are used to NSArray or C arrays: a copy of the array now produces a full and independent copy of all of the elements using an efficient lazy copy implementation. This is a major change for Array, and there are still some performance issues to be addressed. Please !see the Swift Programming Language for more information. (17192555)
The original information on arrays in the Swift book was updated on 7th July 2014 to reflect the beta 3 changes. (If you're using iBooks on a Mac, as I was, you may need to delete and re-download it to pick up the 7th July update—I couldn't get the thing to update automatically.)
IMHO the simplest workaround is simply wrap it in the closure as follows:
let mutableElements = [0,1,2,3]
let reallyImmutable = {[0,1,2,3]}
println(mutableElements)
for i in 0..mutableElements.count { mutableElements[i] *= -1 }
println(mutableElements) // [0, -1, -2, -3]
println(reallyImmutable())
for i in 0..reallyImmutable().count { reallyImmutable()[i] *= -1 }
println(reallyImmutable()) // [0, 1, 2, 3]
println(reallyImmutable()[2]) // 2
let anotherImmutable = { reallyImmutable().map{ $0 * $0 } }
println(anotherImmutable()) // [0, 1, 4, 9]
You pay extra {} on declaration and () for each access but that also makes your code speak for itself.
Dan the Mutable Programmer
P.S. Wrote a wrapper class ImmutableArray.
Seems to be a bug and to be fixed soon.
Cited from Apple dev forum:
Question:
Inconsistency with let for Array and Dictionary
Final answer:
This is considered to be a bug, not a feature, and will be fixed in a later Beta.
-Chris