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

后端 未结 4 1052
臣服心动
臣服心动 2020-12-05 17:16

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

4条回答
  •  失恋的感觉
    2020-12-05 18:07

    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.

提交回复
热议问题