IOS 10 objectForKey ("key") compatibility

I am working on my compatibility with IOS10 and I have a problem while executing this code:

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);
}
        }
    }

I see the results, so JSON is fine, but after I do not see the result in the dictionary. "Here" is never printed on my console. I try to make a breakpoint and it does not go there. I am also trying to use .objectForKey("key"), but the same result: /

Can anybody help me?

If i use results["results"] as? [[String: Any]]

Then

networkPosts [i] as! NSDictionary always fails

+4
source share
1 answer

NSJSONReadingMutableContainers TryJSONObjectWithData

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

, Playground 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");
    }
}
+1

Source: https://habr.com/ru/post/1654035/


All Articles