ios 致命错误:NSArray 元素未能匹配 Swift Array Element 类型

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25484554/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 02:02:12  来源:igfitidea点击:

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

iosswiftios8xcode6-beta6xcode6-beta5

提问by BaSha

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

在升级到 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]

回答by Max MacLeod

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

如果我可以用一些进一步的信息来补充 Teejay 的回答。这个错误:

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:

例如,转换为您的 Swift 数组类型:

    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.

根据 key 访问 aDictionary 中的值"persons",Swift 希望收到一个 Person 类型的数组。这将编译并执行而不会出现问题。

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

但是,稍后在访问myPersonList数组元素时的代码中,如果类型不符合指定的类型(在我的示例Person 中),则执行将因上述错误而崩溃。

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

底线:您几乎可以肯定在演员表中指定了错误的类型。检查您的字典对象以查看它真正包含的内容。

回答by Teejay

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

如果您使用 Cocoa API,您总是会收到一个NSArray,它不是典型的。

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

因此,您需要将该数组转换为典型的 Swift 数组。

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_CLASSobject.

这样,每个数组元素都被转换为一个CUSTOM_CLASS对象。

回答by Chris Conover

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

TL;DR:也是由混合 Xcode 7 和 Xcode 7.1 二进制文件引起的。

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

这已经得到了回答,但我刚刚在 Alamofire 的内部得到了这个错误,用于有效 [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.

就我而言,我使用的是 carthage 并且没有意识到 xcode-select 仍然指向崩溃快乐的 Xcode 7。将 xcode-select 更新到 Xcode 7.1B 解决了我的问题。

回答by Mattia Personaggio Uno Ducci

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 ... inway, 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.

会不会是 swift 类型和 ObjectiveC 的类型之间的冲突?因为我在尝试以[NSMutableDisctionary]with.forEach{}for ... inway循环时遇到了类似的情况,但它给了我同样的错误 ( NSArray element failed to match the Swift Array Element type)。当我将类型更改为[Dictionary<String,Any>]一切正常时。现在,[]在 Swift 中被引入,NS...在 ObjectiveC 中带有前缀的类型。