2016-09-08 36 views
4

我的工作我的IOS10的應用程序的兼容性,我有一個問題,當執行這段代碼:IOS 10 objectForKey(「鑰匙」)的兼容性

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是確定的,但在我看不到字典的結果鍵之後。 「Here」永遠不會打印在我的控制檯上。我試着做出一個斷點,但它並沒有通過那裏。 我也試過.objectForKey("key")但結果相同:/

有人可以幫我嗎?

如果我使用results["results"] as? [[String: Any]]

然後

networkPosts [I]作爲! NSDictionary中總是失敗

+2

後的JSON您收到 –

+0

請向我們展示您的json響應。 –

+0

你的json中有'results'鍵嗎?或者它是'結果'? – Santosh

回答

1

你應該使用TryJSONObjectWithData使數組和字典可變

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

例與遊樂場測試雨燕3時使用NSJSONReadingMutableContainers選項:

+0

我應該使用MutableContainers而不是NSMutableArray嗎? –

+0

我編輯了答案(swift 3 playground兼容) – matsoftware

+0

我有錯誤「使用未解析標識符'JSONSerialization'」。我是否錯過了導入?我只有Foundation和UIKIt –