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 Simple Data Structures Modeling the Current Weather

Kevin Gonzales
Kevin Gonzales
4,638 Points

How does CurrentWeather know to use the p-List dictionary?

import Foundation

struct CurrentWeather { let temaperature: Int let humidity : Int let precipProbability: Int let summary: String

init (weatherDictionary: [String : AnyObject]){
    temaperature = weatherDictionary["temperature"] as! Int
    humidity = weatherDictionary["humidity"] as! Int
    precipProbability = weatherDictionary ["precipProbability"] as! Int
    summary = weatherDictionary ["summary"] as! Int
}

}

So this is our code. Now I understand that weather dictionary gets the value from the p-list and assigns them to our constants, but how does weather dictionary know that we are referring to the p-list and therefore to use the keys "temperature" ect from our p-list dictionary? In other words how does it know were are referring to our p-list? I hope I made the question clear, ask away if you don't understand! I am having difficulties knowing how to ask it lol

Aby Abraham
Aby Abraham
Courses Plus Student 12,531 Points

Yes, Kevin. You are right.

Along the same, Pavan mentioned some new concepts and jargons just like that. What is this "as!" inside init function??

Are you serious, Pavan? Your videos are getting increasingly complicated. You need to explain whats happening here in this video.

3 Answers

Aby Abraham
PLUS
Aby Abraham
Courses Plus Student 12,531 Points

Hi Kevin.

The path of pList is retrieved using NSBundle and the contents of pList is stored to a NSDictionary. The linking of pLists mentioned in the upcoming videos.

NSBundle: An NSBundle object represents a location in the file system that groups code and resources that can be used in a program. NSBundle objects locate program resources, dynamically load and unload executable code, and assist in localization.

NSDictionary: The NSDictionary class declares the programmatic interface to objects that manage immutable associations of keys and values. Use this class or its subclass NSMutableDictionary when you need a convenient and efficient way to retrieve data associated with an arbitrary key.

David Bauer
David Bauer
3,863 Points

I just don't get it (how does it know were are referring to our p-list?) - can someone explain it in maybe an other way to me? That would be awesome! Thanks in advance.

James Hoffman
James Hoffman
6,354 Points

"as!" downcasts the variables that we retrieved from the dictionary as a generic AnyObject type, to a more specific type which is written after "as!". We don't want our data remaining at the higher level type of AnyObject, so we downcast it to a more specific type. Hope this helps Aby!