Welcome to the Treehouse Community
The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Meera Rasamantra
162 PointsBuilding a wether app
I have a few questions, From my code bellow you can see something has changed in swift. before you was putting error alongside with data, options and what so ever. But now you do:
do { let variable = try // your code } catch let error { // Error message }
can you please explain why we doing this way?
and also and did not understand why we are putting "[]" instead of nil?
(data!, options: [] )
here is my code:
class NetworkOperation {
lazy var config: NSURLSessionConfiguration = NSURLSessionConfiguration.defaultSessionConfiguration()
lazy var session: NSURLSession = NSURLSession(configuration: self.config)
let queryURL: NSURL
typealias JSONDictionaryCompletion = ([String: AnyObject]?) -> Void
init(url: NSURL) {
self.queryURL = url
}
func downloadJSONFromURL(completion: JSONDictionaryCompletion) {
let request: NSURLRequest = NSURLRequest(URL: queryURL)
let dataTask = session.dataTaskWithRequest(request) {
(let data, let response, let error) in
// 1. Check HTTP response for successful GET request
if let httpResponse = response as? NSHTTPURLResponse {
switch(httpResponse.statusCode) {
case 200:
// 2. Create JSON object with data
do {
let jsonDictionary = try NSJSONSerialization.JSONObjectWithData(data!, options: []) as! [String: AnyObject]
completion(jsonDictionary)
} catch let error {
print("JSON Serialization failed. Error: \(error)")
}
default:
print("GET request not successful. HTTP status code: \(httpResponse.statusCode)")
}
} else {
print("Error: Not a valid HTTP response")
}
}
}
}
3 Answers

Carson Carbery
9,876 PointsHi Meera,
I can answer your question about the Do and Catch coding. These are changes made for error handling in Swift 2. There is a course called error handling in Swift 2, where these constructs are described. The build a weather app course was made in Swift 1. Hope this helps. Carson

Carson Carbery
9,876 PointsHi Henry,
Yes the options can contain several values in an Array for example: NSJSONSerialization.JSONObjectWithData(data, options: [.MutableContainers, .MutableLeaves])
So to specify no options = an empty array []. Hope that clears it up for you.

Henry Gomez
339 PointsThanks)
Henry Gomez
339 PointsHenry Gomez
339 PointsI get everything except this "[]", can u please explain what does it do ? why we type it instead of nil?