Swift: Cast from Interface type array to Object array crashes

the_critic

I have got an array of objects that conform to an interface

interface SomeInterface{}

and a class that conforms to that interface

class SomeClass : NSObject, SomeInterface{}

Now, I create an array of SomeInterface objects

var myInterfaceObjects : SomeInterface[] = SomeInterface[]()

Then I would like to cast this array to a SomeClass object array

var someClassObjects : SomeClass[] = myInterfaceObjects as SomeClass[] //CRASH!

How can I downcast without a crash ?

fatal error: can't reinterpretCast values of different sizes
(lldb) bt
* thread #1: tid = 0x935ef, 0x001986b8 libswift_stdlib_core.dylib`Swift.reinterpretCast <A, B>(A) -> B + 220, queue = 'com.apple.main-thread', stop reason = EXC_BREAKPOINT (code=EXC_ARM_BREAKPOINT, subcode=0xe7ffdefe)
  * frame #0: 0x001986b8 libswift_stdlib_core.dylib`Swift.reinterpretCast <A, B>(A) -> B + 220
    frame #1: 0x001bebdc libswift_stdlib_core.dylib`Swift.ContiguousArrayBuffer.storesOnlyElementsOfType <A>(Swift.ContiguousArrayBuffer<A>)<B>(B.Type) -> Swift.Bool + 912
    frame #2: 0x001bde08 libswift_stdlib_core.dylib`Swift._arrayCheckedDownCast <A, B>(Swift.Array<A>) -> Swift.Optional<Swift.Array<B>> + 292
    frame #3: 0x00094c84 MyApp`MyApp.MyClass.(data=SomeInterface[]! at 0x27db0870, response=Foundation.NSHTTPURLResponse! at 0x27db086c, error=None, self=<unavailable>) -> (Swift.Bool) -> ()).(closure #1) + 428 at MyClass.swift:81
Jiaaro

Sounds like a compiler bug, you should report it (as Kevin said in the comments)

For now you can try to work around it by casting the objects individually:

var someClassObjects : [SomeClass] = myInterfaceObjects.map { $0 as SomeClass }

edit: updated to the latest swift syntax. Also worth mentioning this bug may have been resolved in beta 3 or beta 4 – I haven't checked

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: Cast from Interface type array to Object array crashes

From Dev

swift: cast incoming json array to dictionary and object

From Dev

swift: cast incoming json array to dictionary and object

From Dev

Cast from an Object ArrayList to Object Array in JSP

From Dev

Java - array types to Object type cast

From Dev

(array) type cast strange behaviour object to array conversion

From Dev

Cast unknown object to generic interface of interface (from initially explicit generic type of generic collection type of type)

From Dev

Cast unknown object to generic interface of interface (from initially explicit generic type of generic collection type of type)

From Dev

Cast object array to generic array

From Dev

Cast array to object

From Dev

load from json array into object array swift

From Dev

Create an Array of Object from an Array of Dictionaries with Swift

From Java

How can I cast an NSMutableArray to a Swift array of a specific type?

From Dev

How to cast an Object to an Object Array

From Dev

Iterate an array w/ explicit object type in Swift

From Dev

Swift: Checking dictionary object type for Array

From Dev

Iterate an array w/ explicit object type in Swift

From Dev

Swift: Checking dictionary object type for Array

From Dev

Array of Data Type Interface

From Dev

Casting From int(primitive type) array to Integer(object type) array

From Dev

Xcode 7 GM Build breaks cast from NSArray to Swift Array

From Dev

How do I cast from a heterogeneous array in Swift 4?

From Dev

Object returned from FieldInfo.GetValue cannot be cast to Array

From Dev

Type cast array elements in for loop

From Dev

Could not cast of type Set to Array

From Dev

cast an object to multidimensional string array

From Dev

Why is it wrong to cast Object[] array?

From Dev

Why is it wrong to cast Object[] array?

From Dev

Java : Cast String Array to an Object

Related Related

  1. 1

    Swift: Cast from Interface type array to Object array crashes

  2. 2

    swift: cast incoming json array to dictionary and object

  3. 3

    swift: cast incoming json array to dictionary and object

  4. 4

    Cast from an Object ArrayList to Object Array in JSP

  5. 5

    Java - array types to Object type cast

  6. 6

    (array) type cast strange behaviour object to array conversion

  7. 7

    Cast unknown object to generic interface of interface (from initially explicit generic type of generic collection type of type)

  8. 8

    Cast unknown object to generic interface of interface (from initially explicit generic type of generic collection type of type)

  9. 9

    Cast object array to generic array

  10. 10

    Cast array to object

  11. 11

    load from json array into object array swift

  12. 12

    Create an Array of Object from an Array of Dictionaries with Swift

  13. 13

    How can I cast an NSMutableArray to a Swift array of a specific type?

  14. 14

    How to cast an Object to an Object Array

  15. 15

    Iterate an array w/ explicit object type in Swift

  16. 16

    Swift: Checking dictionary object type for Array

  17. 17

    Iterate an array w/ explicit object type in Swift

  18. 18

    Swift: Checking dictionary object type for Array

  19. 19

    Array of Data Type Interface

  20. 20

    Casting From int(primitive type) array to Integer(object type) array

  21. 21

    Xcode 7 GM Build breaks cast from NSArray to Swift Array

  22. 22

    How do I cast from a heterogeneous array in Swift 4?

  23. 23

    Object returned from FieldInfo.GetValue cannot be cast to Array

  24. 24

    Type cast array elements in for loop

  25. 25

    Could not cast of type Set to Array

  26. 26

    cast an object to multidimensional string array

  27. 27

    Why is it wrong to cast Object[] array?

  28. 28

    Why is it wrong to cast Object[] array?

  29. 29

    Java : Cast String Array to an Object

HotTag

Archive