This should be pretty simple but it's doing my head in. I'm doing a post request with swift and I get some Json data back:
Optional({
authString = "bWF0dEB0YXN0";
data = {
x = asd;
y = 1234;
};
success = 1;
})
I can successfully get authString
and success
but I can't seem to be able to parse data
this works:
let auth = parseJSON["authString"] as? String
This doesn't:
let dataArray = parseJSON["data"] as? NSDictionary
println(dataArray["x"])
This returns nil:
let dataArray = parseJSON["data"] as? Array<NSDictionary>
println(dataArray)
Try:
let dataArray = parseJSON["data"] as? NSDictionary
println(dataArray?["x"])
// ^ HERE
Because dataArray
is Optional<NSDictionary>
, you have to use "Optional Chaining" syntax.
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.