I'm trying to convert a JSON response (from an NSUrlSession
) into an array that I can use.
It's weird, this was working last night. However I now have a build error saying "ambiguous use of subscript".
let url = NSURL(string: "http://192.168.0.8/classes/main.php?fn=dogBoardingGet")
let task = NSURLSession.sharedSession().dataTaskWithURL(url!) {(data, response, error) in
print(NSString(data: data!, encoding: NSUTF8StringEncoding))
//var boardings = [String]()
do {
let json = try NSJSONSerialization.JSONObjectWithData(data!, options: .AllowFragments)
if let theDogs = json[0] as? [[String: AnyObject]] {
for dog in theDogs {
if let ID = dog["ID"] as? String {
print(ID + " Safe")
let thisDog = Dog(name: (dog["Name"] as? String)!, surname: (dog["Surname"] as? String)!, id: (dog["ID"] as? String)!, boarding: true)
let newIndexPath = NSIndexPath(forRow: self.dogs.count, inSection: 0)
self.dogs.append(thisDog)
self.tableView.insertRowsAtIndexPaths([newIndexPath], withRowAnimation: .Bottom)
}
}
}
} catch {
print("error serializing JSON: \(error)")
}
// print(names) // ["Bloxus test", "Manila Test"]
}
task.resume()
The error is on this line: if let theDogs = json[0] as? [[String: AnyObject]] {
.
From what I could tell while looking other questions, the error is because of AnyObject
, so I tried to change it to [String: String]
but I still get the same error.
Can anyone see the cause of this error?
Extra Information
The JSON response being received from the server:
[[{"ID":"47","Name":"Sparky","Surname":"McAllister"}]]