简体   繁体   English

无法转换类型为&#39;Swift._NativeDictionaryStorageOwner的值 <Swift.String, Swift.AnyObject> &#39;(0x40c2134)到&#39;NSArray&#39;(0x383bb8d8)

[英]Could not cast value of type 'Swift._NativeDictionaryStorageOwner<Swift.String, Swift.AnyObject>' (0x40c2134) to 'NSArray' (0x383bb8d8)

Upon revisiting some code that seemed to work with Xcode7.3, I noticed that I am getting a Could not cast value of type 'Swift._NativeDictionaryStorageOwner' (0x40c2134) to 'NSArray' (0x383bb8d8). 在重新访问一些似乎可与Xcode7.3一起使用的代码后,我注意到我无法将类型'Swift._NativeDictionaryStorageOwner'(0x40c2134)的值强制转换为'NSArray'(0x383bb8d8)。

userdata.data = userOtherInfo[indexPath.row] as! userdata.data = userOtherInfo [indexPath.row]为! NSArray NSArray

Here is the section of code that appears to be the problem: 这是似乎是问题所在的代码部分:

    func collectionView(collectionView: UICollectionView,        didSelectItemAtIndexPath indexPath: NSIndexPath) {

    let userdata:SubContentsViewController! = SubContentsViewController()
    let storyboard = UIStoryboard(name: "SubContentsViewController", bundle: nil)
    let subContentsVC = storyboard.instantiateViewControllerWithIdentifier("SubContentsViewController") as! SubContentsViewController
    userdata.data = userOtherInfo[indexPath.row] as! NSArray
    self.navigationController?.pushViewController(subContentsVC, animated: true)
}

As you wrote in your comment 正如您在评论中所写

rows = value["search_result"] as? [[String: AnyObject]]
self.userOtherInfo = rows

rows / userOtherInfo is an array of dictionaries ( [[String: AnyObject]] ) on success. rows / userOtherInfo是成功的字典数组[[String: AnyObject]] )。
That's what the error message says : 那就是错误消息说的:

Cannot cast Dictionary to Array. 无法将字典转换为数组。

The solution is very very simple. 解决方案非常非常简单。

Since the compiler already knows that userOtherInfo contains an array of [String: AnyObject] you need no casting at all. 由于编译器已经知道userOtherInfo包含[String: AnyObject]的数组,因此根本不需要强制转换。

userdata.data = userOtherInfo[indexPath.row] // object is [String:AnyObject]

Make sure that userdata.data is declared as [String:AnyObject] or a more specific dictionary. 确保将userdata.data声明为[String:AnyObject]或更具体的字典。

This solution is working fine for me. 这个解决方案对我来说很好。 let mutableDict = NSMutableDictionary(dictionary: newDictionaryResult!) 让mutableDict = NSMutableDictionary(dictionary:newDictionaryResult!)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法转换类型为&#39;Swift.Optional的值 <Swift.AnyObject> &#39;(0x7f9d44715db0)到&#39;NSArray&#39;(0x60000004f4f8) - Could not cast value of type 'Swift.Optional<Swift.AnyObject>' (0x7f9d44715db0) to 'NSArray' (0x60000004f4f8) 无法将类型&#39;()&#39;(0x2c2fb50)的值强制转换为&#39;Swift.AnyObject - Could not cast value of type '()' (0x2c2fb50) to 'Swift.AnyObject 无法转换类型为&#39;Swift._SwiftDeferredNSDictionary&#39;的值 <Swift.String, Swift.String> ”到“ NSMutableDictionary” - Could not cast value of type 'Swift._SwiftDeferredNSDictionary<Swift.String, Swift.String>' to 'NSMutableDictionary' 无法转换类型为&#39;Swift._ContiguousArrayStorage的值 <Swift.AnyObject> &#39;至&#39;NSMutableArray&#39; - could not cast value of type 'Swift._ContiguousArrayStorage<Swift.AnyObject>' to 'NSMutableArray' 无法转换类型为&#39;Swift.Array的值 <Swift.Dictionary<Swift.String, Swift.String> &gt;”()到“ Swift.Dictionary” <Swift.String, Any> &#39;() - Could not cast value of type 'Swift.Array<Swift.Dictionary<Swift.String, Swift.String>>' () to 'Swift.Dictionary<Swift.String, Any>' () 无法将“SwiftyJSON.JSON”类型的值转换为“Swift.String” - Could not cast value of type 'SwiftyJSON.JSON' to 'Swift.String' 无法转换类型“Swift.Array”的值<Any> &#39; 到 &#39;Swift.Dictionary<Swift.String, Any> &#39; - Could not cast value of type 'Swift.Array<Any>' to 'Swift.Dictionary<Swift.String, Any>' 无法将“__NSCFNumber”()类型的值快速转换为“NSArray” - Could not cast value of type '__NSCFNumber' () to 'NSArray' swift 无法将类型_NCSFDictionary的值转换为NSArray Swift 2 - Could not cast value of type _NCSFDictionary to NSArray Swift 2 Swift:无法将类型为“ UIImageView”的值转换为“ NSArray” - Swift: could not cast value of type 'UIImageView' to 'NSArray'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM