Reputation: 262
Im learning on how to write iOS code and I'm trying to write a weather application. Part of the JSON code that I retrieve from yahoo weather is:
`"forecast":[
{
"code":"24",
"date":"2 Mar 2015",
"day":"Mon",
"high":"39",
"low":"16",
"text":"Partly Cloudy/Wind"
}
]`
But for some reason, that part of the code has "[" and "]" symbol in it. Therefore my code is not able to get the data and store it in the NSDictionary. The iOS swift code I use to get data is:
`if let forecast = item["forecast"] as? NSDictionary{
let highDay: AnyObject = forecast["high"]!
let lowDay: AnyObject = forecast["low"]!
high = String(highDay as NSString)
self.high.extend("˚")
println(high)
low = String(lowDay as NSString)
self.low.extend("˚")
}`
Im not having any issues with processing data blocks that doesn't have the "[" and "]" symbols. But I couldn't figure out this one. Are there any workarounds for this issue?
Upvotes: 0
Views: 241
Reputation: 3352
forecast:[...] indicates that you are in a dictionary. That value for the forecast key in that dictionary item["forecast"]
is not a dictionary, but an array. In JSON, [ ] is an array and { } is a dictionary.
In your exampled, the forecast array has only on item which is a dictionary. To get it you could try:
if let forecastArray = item["forecast"] as? NSArray {
if let forecast = forecastArray[0] as? NSDictionary {
}
}
Upvotes: 2