fatal error: NSArray element failed to match the Swift Array Element type
Asked Answered
L

4

36

Suddenly I'v started getting run time error as,

fatal error: NSArray element failed to match the Swift Array Element type

I'v declared my array as,

var myArray : [CUSTOM_CLASS] = [CUSTOM_CLASS]()

Now, in my server response success block I have,

self.myArray = dicResponse["data"]! as Array

println(self.myArray) // FATAL ERROR HERE

Which was working perfect before upgrading to Xcode6 Beta6

FYI : dicResponse["data"]! // is verified as valid

(Sorry to pointing wrong place before!)

SOLVED :

Dont know but I'd made some changes and it works,

var myArray = [AnyObject]()

self.myArray = dicResponse["data"]! as [AnyObject]
Liebknecht answered 25/8, 2014 at 11:8 Comment(5)
Can you post more info?Coligny
Yes, please, post more code.Sheedy
@Liebknecht please accept one answer if it helped, or write and accept your own for future reference, instead of writing your solution in the question.Sheedy
I had the same issue, but my solution was the error in subclassing a Parse PFObject - in the new Swift 1.7.1 parse release.Endaendall
Had the same error using Parse as well. @Liebknecht your solution worked perfectly, thanks for the update!Saberhagen
D
20

If I could supplement Teejay's answer with some further information. This error:

fatal error: NSArray element failed to match the Swift Array Element type

is caused by a type mismatch.

For example having cast to your Swift array type:

    myPersonList = aDictionary["persons"] as [Person]

Accessing the value in aDictionary based upon key "persons", Swift expects to receive an array of type Person. This will compile and will execute without issue.

However, later in your code when accessing the myPersonList array element, if the type is not as specified - in my example Person - then execution will crash with the error above.

Bottom line: you almost certainly have specified the wrong type in the cast. Examine your dictionary object to see what it really contains.

Drakensberg answered 11/9, 2014 at 10:47 Comment(1)
This answer worked for me! I thought I had run into an unresolved issue because I was using a class in two different frameworks and trying to archive/unarchive the objects on the watch/ios, and then got this error message. I thought the reason it was complaining was because it was actually two different frameworks. However, the real problem was that I was accidentally wrapping my array of objects in another array.Sericin
S
4

If you are working with Cocoa APIs you always receive a NSArray, which is not typified.

So, you need to cast that array to a Typified Swift Array.

You should be able to compile this code:

var myArray : [CUSTOM_CLASS] = [CUSTOM_CLASS]()

self.myArray = dicResponse["data"]! as [CUSTOM_CLASS]

This way, each array element is casted to a CUSTOM_CLASS object.

Sheedy answered 25/8, 2014 at 15:24 Comment(0)
R
2

Could it be a conflict between swift type and ObjectiveC's one? Because I experienced a similar situation trying to loop on a [NSMutableDisctionary] both with .forEach{} and for ... in way, but it gave me your same error (NSArray element failed to match the Swift Array Element type). When I changed the type to [Dictionary<String,Any>] all worked well. Now, [] was introduced in Swift, and types with prefix NS... in ObjectiveC.

Ronnie answered 21/3, 2019 at 9:1 Comment(0)
S
1

TL;DR: Also caused by mixing Xcode 7 and Xcode 7.1 binaries.

This has already been answered, but I just got this error in the bowels of Alamofire for an array cast of a valid [String].

In my case, I was using carthage and had not realized that xcode-select was still pointing at the crash-happy Xcode 7. Updating xcode-select to Xcode 7.1B fixed my problem.

Snowbound answered 24/9, 2015 at 16:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.