Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you create an immutable array in Swift?

Tags:

arrays

swift

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.

like image 248
algal Avatar asked Jun 06 '14 21:06

algal


People also ask

How do you create an immutable array?

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.

What is immutable array in Swift?

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.

How do you make an array mutable in Swift?

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.

What is an immutable array?

An immutable array or object is a unique copy of the original that, when manipulated, does not affect the original.


2 Answers

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.)

like image 168
Matt Gibson Avatar answered Sep 16 '22 11:09

Matt Gibson


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

like image 25
eonil Avatar answered Sep 20 '22 11:09

eonil