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]
But sadly this actually produces a mutable, fixed-size array. This mutability creates the usual puzzles with unsuspected aliasing and functions mutating their arguments:
let outterArray = [myArray, myArray] outterArray[0][0] = 2000 outterArray //=> [[2000,2,3],[2000,2,3]] surprise! func notReallyPure(arr:Int[]) -> () { arr[0] = 3000 } notReallyPure(myArray) myArray // => [3000,2,3]
Not much better than C.
If I want immutability, is the best option really to wrap it in an NSArray
like so:
let immutableArray = NSArray(myArray: [1,2,3])
That seems nuts. What am I missing here?
UPDATE (2015-07-26):
This question dates from the very early days of Swift. Swift has since then been updated so that immutable arrays are actually immutable, as answers below indicate.
There is one way to make an immutable array in Java: final String[] IMMUTABLE = new String[0]; Arrays with 0 elements (obviously) cannot be mutated. This can actually come in handy if you are using the List.
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.
If you assign a created array to a variable, then it is always mutable, which means you can change it by adding, removing, or changing its items; but if you assign an array to a constant, then that array is immutable, and its size and contents cannot be changed.
An immutable array or object is a unique copy of the original that, when manipulated, does not affect the original.
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.)
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With