Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS Build a Weather App with Swift (Retired) Data Modeling With Structures Cleaning Up Our Date

Keith Michelson
Keith Michelson
3,395 Points

How could you print other JSON properties outside of Currently?

Say I wanted to print the properties of a certain time? Or how do you drill down deeper in nested items? Thanks.

2 Answers

I was wondering the same... I was able to get to the Hourly Data. But not as I had originally expected to.

Originally I tried to get the data for each hour by just doing the following weatherDictionary["hourly"]["data"][0]

But that did not work.

In the end, I had to get the hourly as an optional and then get the data as an optional downcast to an NSArray. Then I was able to access the individual hour data.

So assuming that you already have the weatherDictionary as an NSDictionary from the video, you can do the following:

if let hourlyWeatherArray = weatherDictionary["hourly"]?["data"] as? NSArray {
    println("First Hour")
    println(hourlyWeatherArray[0])
    println("Second Hour")
    println(hourlyWeatherArray[1])
}
Keith Michelson
Keith Michelson
3,395 Points

Thanks for the reply, I will test that out!