Swift optional Array property is immutable?

Eric Gratta

I am constructing an array of booleans to store the state of the sections in a UICollectionView. It is a variable stored as a property of my UIViewController:

var _weekSelections : Array<Bool>!

Then, in a function called by loadView(), I construct the array and assign a value to the first index:

_weekSelections = Array<Bool>(count:_weekCount, repeatedValue:false)
_weekSelections[0] = true

The value at index 0 remains false! The array is constructed, and has multiple elements, but any assignment that I make to an index does not affect the value stored at that index, even if I check the value on the very next line of code. I know that Swift makes a copy of an array if I perform an action that may change its length, but I don't think this is a case where a copy would me made. The only way I can get any value to change is if I manually create a copy as follows:

var copy = _weekSelections
copy[0] = true
_weekSelections = copy

Am I missing something obvious or could this be a strange bug?

Matt Gibson

For the sake of having my code on SO rather than Pastebin, here's my observation. This looks like some kind of bug or unexpected behaviour when using an optional array in a Swift class derived from an Objective C class. If you use a plain Swift class, this works as expected:

class Foo {
    var weekSelections: Array<Bool>!
    func test() {
        weekSelections = Array<Bool>(count: 10, repeatedValue: false)
        weekSelections[0] = true;
        println(weekSelections[0]) // Prints "true"
    }
}

var foo = Foo()
foo.test()

However, if you derive Foo from NSObject:

import Foundation

class Foo : NSObject { // This derivation is the only difference from the code above
    var weekSelections: Array<Bool>!
    func test() {
        weekSelections = Array<Bool>(count: 10, repeatedValue: false)
        weekSelections[0] = true;
        println(weekSelections[0]) // Prints "false"
    }
}

var foo = Foo()
foo.test()

Even in this case, if you do your weekSelections initialisation in an initialiser, then it works:

class Foo : NSObject {
    var weekSelections: Array<Bool>!
    init() {
        weekSelections = Array<Bool>(count: 10, repeatedValue: false)
        weekSelections[0] = true;
        println(weekSelections[0]) // Prints "true"
    }
}

var foo = Foo()

Personally, I'd say that this is a bug. I can't see anything in any documentation that would explain the difference in behaviour when derived from NSObject.

I also can't see anything that says that optional array properties would be immutable. This would be especially strange when you consider that "immutable" arrays are actually mutable in Swift, i.e. this:

// Use "let" to declare an "immutable" array
let weekSelections = Array<Bool>(count: 10, repeatedValue: false)
weekSelections[0] = true;
println(weekSelections[0]); // Prints "true"; arrays are never really "immutable" in Swift

...works fine, and is currently documented as being valid, even if it seems a bit odd.

Personally, I'd use whatever workaround you can and raise a bug with Apple, to see what light they can shed.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Swift optional Array property is immutable?

From Dev

Swift: Reduce optional array of models to single Bool by optional property

From Dev

optional closure property in Swift

From Dev

Swift optional property error

From Dev

swift - sort an array of objects by their optional boolean property without force unwrapping

From Dev

Cannot use mutating member on immutable value array is a get-only property swift 3

From Dev

Swift: optional array count

From Dev

Optional chaining and Array in swift

From Dev

Swift: setting an optional property of a protocol

From Dev

Cocoa Binding to an optional property in swift

From Dev

Initialize property of type immutable dictionary in Swift init()

From Dev

Why is an optional array not enumerable in Swift?

From Dev

Optional in Swift, return count of array

From Dev

Swift Optional Array Index Error

From Dev

How do you create an immutable array in Swift?

From Dev

Create a Dictionary as a optional property using Swift

From Dev

Why is the 'view' property of UIViewController not optional in Swift?

From Dev

Swift optional or Implicit property on UIViewController subclass

From Dev

Swift - how to make inner property optional

From Dev

Explicitly unwrapping an assignment to an optional property in Swift 2.2

From Dev

Swift Array intersection by property

From Dev

How to store immutable arrays in a variable stored property in Swift?

From Dev

How do I initialize an immutable Swift property to a computed value ?

From Dev

Optional array vs. empty array in Swift

From Dev

Are optional arrays designed to be "immutable"?

From Dev

immutable.js map over array (List) and add a property

From Dev

Array initializer shorthand with optional values in Swift

From Dev

Swift 2 parse Json as Optional to array

From Dev

Swift Array optional Type and subscripting (Beta 3)

Related Related

  1. 1

    Swift optional Array property is immutable?

  2. 2

    Swift: Reduce optional array of models to single Bool by optional property

  3. 3

    optional closure property in Swift

  4. 4

    Swift optional property error

  5. 5

    swift - sort an array of objects by their optional boolean property without force unwrapping

  6. 6

    Cannot use mutating member on immutable value array is a get-only property swift 3

  7. 7

    Swift: optional array count

  8. 8

    Optional chaining and Array in swift

  9. 9

    Swift: setting an optional property of a protocol

  10. 10

    Cocoa Binding to an optional property in swift

  11. 11

    Initialize property of type immutable dictionary in Swift init()

  12. 12

    Why is an optional array not enumerable in Swift?

  13. 13

    Optional in Swift, return count of array

  14. 14

    Swift Optional Array Index Error

  15. 15

    How do you create an immutable array in Swift?

  16. 16

    Create a Dictionary as a optional property using Swift

  17. 17

    Why is the 'view' property of UIViewController not optional in Swift?

  18. 18

    Swift optional or Implicit property on UIViewController subclass

  19. 19

    Swift - how to make inner property optional

  20. 20

    Explicitly unwrapping an assignment to an optional property in Swift 2.2

  21. 21

    Swift Array intersection by property

  22. 22

    How to store immutable arrays in a variable stored property in Swift?

  23. 23

    How do I initialize an immutable Swift property to a computed value ?

  24. 24

    Optional array vs. empty array in Swift

  25. 25

    Are optional arrays designed to be "immutable"?

  26. 26

    immutable.js map over array (List) and add a property

  27. 27

    Array initializer shorthand with optional values in Swift

  28. 28

    Swift 2 parse Json as Optional to array

  29. 29

    Swift Array optional Type and subscripting (Beta 3)

HotTag

Archive