Angela Yu Course Part 16 (lessons 154 to 156)
Yes! We're continuing with Angela's Course! Lots of new concepts we're being introduced to via APIs but all good stuff. Useful in the future - lots of possibilities in terms of accessing data from websites.
JSON and how to Parse it
JSON and how to Parse it
let weatherJSON : JSON = response.result.value
The issue here is that we need to force unwrap the value here. So Swift has given this automatic correction:
let weatherJSON : JSON = response.result.value as! JSON
Angela's correction though is this -
let weatherJSON : JSON = JSON(response.result.value!)
OK so the build is succeeding but the 'location unavailable' keeps coming up on the main screen. No idea why.
OK I can't do the next bit - copy the output in the console into the JSON website as I only have error output. That's a shame!
The JSON data shows it in key value pairs - like a dictionary. The JSON editor online makes it into a more human readable format!
Remember JSON is Java Script Object Notation. That's actually used in a number of different programming languages.
A closure - a function within a function. Interesting!
So whenever 'in' is involved, you must use 'self' as the keyword before a dot then the function being used.
Creating a Weather Data Model
Various things here.
Command and forward slash - gets rid of comment markings over multiple lines. Neat!
The switch statement has already been typed - so the different weather code (id) has to be checked through, then shows the type of weather - the image already saved - that matches up.
Optional binding - I'm slowly but surely getting this. In situations where we may not be able to get the data, doing an if let is great because force unwrapping is only for when we 100% have the value. Then if it is nil, then we can put in an error message.
Update the User Interface
So I've done everything but still not working. It would take hours to identify any typos and I reckon I've done everything right anyway.
*I'm going to stop there!
One of the key things that has come up is that things just don't work sometimes. That's fine! I can still code along and learn along the way! What I'm picking up from this will surely in potentially years to come. So the fact that it's not working is fine - I've tried to follow everything carefully. At the end, I'll check the project file Angela has done and see if that one works. If not, again - it's what I learn along the way that counts!
Comments
Post a Comment