How do you create an immutable array in Swift?

后端 未结 5 782
臣服心动
臣服心动 2020-11-30 04:16

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]

相关标签:
5条回答
  • 2020-11-30 04:24

    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!

    0 讨论(0)
  • 2020-11-30 04:27

    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.

    0 讨论(0)
  • 2020-11-30 04:36

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

    0 讨论(0)
  • 2020-11-30 04:37

    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.

    0 讨论(0)
  • 2020-11-30 04:47

    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

    0 讨论(0)
提交回复
热议问题