Optional chaining and Array in swift

MatterGoal

Let's take these two simple classes to show my problem:

class Object{  
    var name:String?
    // keep it simple... and useless
}

class TestClass {
    var objects:AnyObject[]? 

    func initializeObjects (){
        objects?.insert(Object(), atIndex:0) // Error
        objects?.insert(Object(), atIndex:1) // Error
        objects?.insert(Object(), atIndex:2) // Error
    }
}

With this implementation I get 3 errors Could not find member 'insert' where I try to add object into the objects array.

Now, if I remove the optional from objects definition and the optional chain in initializeObjects it works with no problem (here the working code)

class Object{
    var name:String?
}

class TestClass {
    var objects:AnyObject[] = AnyObject[]() // REMOVE optional and initialize an empty array

    func initializeObjects (){
        objects.insert(Object(), atIndex:0) // Remove Opt chaining 
        objects.insert(Object(), atIndex:1) // Remove Opt chaining
        objects.insert(Object(), atIndex:2) // Remove Opt chaining
    }
}

I can't understand what is wrong in the first implementation. I thought it checks with objects? if objects is not nil and at this point it adds an element using insert:atIndex:. But I'm probably wrong -.-

Sulthan

Arrays in Swift are structs and structs are value types.
Optionals in Swift are actually enums (Optional<T> or ImplicitlyUnwrappedOptional<T>).

When you are unwrapping an optional (implicitly or explicitly) of a value type, what you get is actually a constant copy of the struct. And you can't call mutating methods on a constant struct.

Executing objects?.insert(Object(), atIndex:0) basically means this:

if let tmp = objects {
    tmp.insert(Object(), atIndex:0)
}

As a workaround, you need to assign the unwrapped value to a variable and then assign the variable back to your optional property. That's how value types work.

This is reproducible for any struct, not only Arrays:

struct S {
    var value: Int = 0
}

var varS: S = S()
varS.value = 10 //can be called

let constS: S = S()
constS.value = 10 //cannot be called - constant!

var optionalS: S? = S()
optionalS?.value = 10 //cannot be called, unwrapping makes a constant copy!

//workaround
if optionalS {
    var tmpS = optionalS!
    tmpS.value = 10
    optionalS = tmpS
}

Some relevant discussion here: https://devforums.apple.com/thread/233111?tstart=60

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

Optional chaining (?.) in nashorn

From Java

How to use optional chaining with array in Typescript?

From Dev

Swift optional chaining doesn't work in closure

From Dev

Why is an optional array not enumerable in Swift?

From Dev

Swift optional Array property is immutable?

From Dev

Swift: optional array count

From Dev

swift optional chaining with cast

From Dev

Optional in Swift, return count of array

From Dev

Optional Chaining in JavaScript

From Dev

Optional Chaining or ternary expression in Swift?

From Dev

Optional Chaining Not Working As Expected

From Dev

Optional array vs. empty array in Swift

From Dev

Optional Chaining returning an Int

From Dev

Accessing boolValue in a NSNumber var with optional chaining (in Swift)

From Dev

Swift Optional Array Index Error

From Dev

Is Swift optional chaining always done with the if let construction, or is it just done using a question mark with an optional?

From Dev

How to use optional chaining while searching through a dictionary in swift?

From Dev

Optional chaining used in left side of assignment in Swift

From Dev

In swift, why can I set a computed property of a polymorphic variable via optional chaining, but not on an unwrapped optional?

From Dev

Is there something like the swift optional chaining in javascript?

From Dev

Is Swift optional chaining lazily evaluated left-to-right?

From Dev

Swift 3.0 Optional Chaining

From Dev

Optional chaining with Swift strings

From Dev

Chaining Optional.orElseThrow

From Dev

Swift optional Array property is immutable?

From Dev

an unexpected error while writing a piece of code from Swift programming by Apple(optional chaining)

From Dev

Optional chaining for constructor calls?

From Dev

In swift, why can I set a computed property of a polymorphic variable via optional chaining, but not on an unwrapped optional?

From Dev

optional chaining in Swift 3: why does one example work and not the other?

Related Related

  1. 1

    Optional chaining (?.) in nashorn

  2. 2

    How to use optional chaining with array in Typescript?

  3. 3

    Swift optional chaining doesn't work in closure

  4. 4

    Why is an optional array not enumerable in Swift?

  5. 5

    Swift optional Array property is immutable?

  6. 6

    Swift: optional array count

  7. 7

    swift optional chaining with cast

  8. 8

    Optional in Swift, return count of array

  9. 9

    Optional Chaining in JavaScript

  10. 10

    Optional Chaining or ternary expression in Swift?

  11. 11

    Optional Chaining Not Working As Expected

  12. 12

    Optional array vs. empty array in Swift

  13. 13

    Optional Chaining returning an Int

  14. 14

    Accessing boolValue in a NSNumber var with optional chaining (in Swift)

  15. 15

    Swift Optional Array Index Error

  16. 16

    Is Swift optional chaining always done with the if let construction, or is it just done using a question mark with an optional?

  17. 17

    How to use optional chaining while searching through a dictionary in swift?

  18. 18

    Optional chaining used in left side of assignment in Swift

  19. 19

    In swift, why can I set a computed property of a polymorphic variable via optional chaining, but not on an unwrapped optional?

  20. 20

    Is there something like the swift optional chaining in javascript?

  21. 21

    Is Swift optional chaining lazily evaluated left-to-right?

  22. 22

    Swift 3.0 Optional Chaining

  23. 23

    Optional chaining with Swift strings

  24. 24

    Chaining Optional.orElseThrow

  25. 25

    Swift optional Array property is immutable?

  26. 26

    an unexpected error while writing a piece of code from Swift programming by Apple(optional chaining)

  27. 27

    Optional chaining for constructor calls?

  28. 28

    In swift, why can I set a computed property of a polymorphic variable via optional chaining, but not on an unwrapped optional?

  29. 29

    optional chaining in Swift 3: why does one example work and not the other?

HotTag

Archive