IOS 10 objectForKey(“key”)的兼容性

IOS 10 objectForKey(“key”)的兼容性,第1张

概述我正在研究我的应用程序与 IOS10的兼容性,我在执行此代码时遇到问题: if let results = NSJSONSerialization.TryJSONObjectWithData(data, options: []) as? NSDictionary { print(" results : \(results)"); if let networkPosts = res 我正在研究我的应用程序与 IOS10的兼容性,我在执行此代码时遇到问题:

if let results = NSJsONSerialization.TryJsONObjectWithData(data,options: []) as? NSDictionary {    print(" results : \(results)");    if let networkposts = results["results"] as? NSMutableArray {        print(" here");                for i in 0 ..< networkposts.count {                    let post:Post = Post(postDictionary: networkposts[i] as! NSDictionary,context: (UIApplication.sharedApplication().delegate as! AppDelegate).coreDataHelper.managedobjectContext);}        }    }

我可以看到结果,所以JsON没问题,但是在我看不到字典的结果键之后.
“这里”永远不会打印在我的控制台上.我试着制作一个断点,同样它不会通过那里.
我也尝试使用.objectForKey(“key”)但结果相同:/

有谁可以帮助我吗 ?

如果我使用结果[“结果”]作为? [[String:Any]]

然后

networkposts[i] as! NSDictionary always fails

解决方法 在使用TryJsONObjectWithData使数组和字典可变时,您应该使用NSJsONReadingMutableContainers选项

https://developer.apple.com/library/ios/documentation/Foundation/Reference/NSJSONSerialization_Class/#//apple_ref/c/tdef/NSJSONReadingOptions

使用Playground for Swift 3测试的示例:

let JsonString = "{\"name\":\"Mattia\",\"iosDevices\":[\"iPhone6\",\"iPad Air 2\",\"iPhone6+\"]} let JsonData = JsonString.data(using: String.EnCoding.utf8,allowLossyConversion: false)!if let results = try JsONSerialization.JsonObject(with: JsonData,options: [.mutableContainers]) as? NSDictionary {    print(" results : \(results)");    if let networkposts = results["iosDevices"] as? NSMutableArray {        print(" here");    }}
总结

以上是内存溢出为你收集整理的IOS 10 objectForKey(“key”)的兼容性全部内容,希望文章能够帮你解决IOS 10 objectForKey(“key”)的兼容性所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1030885.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-23
下一篇 2022-05-23

发表评论

登录后才能评论

评论列表(0条)

保存