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]]
然后
解决方法 在使用TryJsONObjectWithData使数组和字典可变时,您应该使用NSJsONReadingMutableContainers选项networkposts[i] as! NSDictionary always fails
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”)的兼容性所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)