问题描述
突然我开始得到运行时错误,
Suddenly I'v started getting run time error as,
fatal error: NSArray element failed to match the Swift Array Element type
我将我的数组声明为,
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
升级到Xcode6 Beta6之前哪个工作正常?
Which was working perfect before upgrading to Xcode6 Beta6
FYI : dicResponse["data"]! // is verified as valid
(抱歉指向错误的地方!)
(Sorry to pointing wrong place before!)
已解决:
不知道但我做了一些更改并且有效,
Dont know but I'd made some changes and it works,
var myArray = [AnyObject]()
self.myArray = dicResponse["data"]! as [AnyObject]
推荐答案
如果我可以补充Teejay的答案一些进一步的信息此错误:
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.
例如,对你的Swift数组类型进行强制转换:
For example having cast to your Swift array type:
myPersonList = aDictionary["persons"] as [Person]
根据键<$访问aDictionary中的值c $ c>人员,Swift希望收到 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.
但是,稍后在代码中访问 myPersonList
数组元素时,如果类型不符合规定 - 在我的示例人 - 然后执行将因上述错误而崩溃。
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.
底线:你几乎肯定在演员表中指定了错误的类型。检查你的字典对象,看看它真正包含的内容。
这篇关于致命错误:NSArray元素无法匹配Swift数组元素类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!