I am very new to swift, I used to code in objective C, but Now I am facing alot of problems while development in Swift. Right now I have json file imported in project
let file = Bundle.main.url(forResource: "test", withExtension: "json")
do {
let data = try Data(contentsOf: file!)
//let json = try JSONSerialization.jsonObject(with: data, options: JSONSerialization.ReadingOptions.mutableContainers) as! NSDictionary
let json:NSArray = try (JSONSerialization.jsonObject(with: data, options:JSONSerialization.ReadingOptions.mutableContainers) as? NSArray)!
print(json[0])
} catch let error as NSError {
print("Failed to load: \(error.localizedDescription)")
}
It gives output like
{
Correct = 1;
EA = "50 Days ";
EB = "40 days";
EC = "90 days";
ED = "30 days";
}
And i Need to access only Content with key "EA".
previously with objective C, I used to access it like
[[json objectAtIndex:0] objectForKey:@"EA"]
Now when I am trying to access it like
json[0].objectForKey("EA") as? String
it says
Value of type 'Any' has no member 'objectForKey'
What should I do?
Swift 2
(json[0] as! NSDictionary).objectForKey("EA") as? String
Swift 3 & 4
(json[0] as! NSDictionary).object(forKey: "EA") as? String
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.