iOS 10 NSMutableDictionary和NSMutableArray无法像以前一样工作 [英] iOS 10 NSMutableDictionary and NSMutableArray not working as before

查看:43
本文介绍了iOS 10 NSMutableDictionary和NSMutableArray无法像以前一样工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

似乎随着iOS 10的发布,一些事情已经坏了.对我来说主要的是使用NSMutableDictionary和NSMutableArray.两者似乎都不再能够解析JSON字符串,而是给出零,而在iOS 10之前的版本中,它们按预期填充.我发现的唯一解决方法是分别使用NSDictionary和NSArray,然后使用init方法进行回退.例如:

It seems with the release of iOS 10, a few things have broken. The major one for me has been the use of NSMutableDictionary and NSMutableArray. Both no longer seem to be able to parse a string of JSON and instead give out a nil while in pre iOS 10 they populated as expected. The only way around this I've found is to use NSDictionary and NSArray respectively and then use the init methods to cast back. For example:

let json = "{ \"code\": \"abcde\", \"name\": \"JP Morgan\" }"
json as! NSMutableDictionary // gives nil
NSMutableDictionary(dictionary: json as! NSDictionary) // works :)

let json = "[{ \"code\": \"abcde\", \"name\": \"JP Morgan\" }]"
json as! NSMutableArray // gives nil
NSMutableArray(array: json as! NSArray) // works :)

我想知道为什么吗?

我希望这可以帮助某人解决他们的问题...

And I hope this helps someone solve their issue...

推荐答案

基础类型 NSMutableArray/NSMutableDictionary 与Swift的对应类型无关,并且不能与从字面上创建的Swift类型进行桥接/强制转换.但这在Swift 3中并不新鲜.

The Foundation types NSMutableArray / NSMutableDictionary are not related to the Swift counterparts and cannot be bridged / coerced from a literally created Swift type. But that's not new in Swift 3.

基本上请勿在Swift中使用 NSMutableArray/NSMutableDictionary ,除非您别无选择,例如与一些底层CoreFoundation API交互.与 var 一起使用的本机 Array / Dictionary 类型提供相同的功能(值和引用语义除外),此外还包含对象.

Basically do not use NSMutableArray / NSMutableDictionary in Swift unless you have absolutely no choice for example interacting with a few low level CoreFoundation API. The native Array / Dictionary types used with var provide the same functionality (except value vs. reference semantics) and in addition the types of the containing objects.

这篇关于iOS 10 NSMutableDictionary和NSMutableArray无法像以前一样工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆