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
Thomas Nilsen
14,957 PointsNetworking function in Swift.
I have the following function:
typealias completionBlock = (venues: AnyObject?, error: NSError?) -> ()
func performVenueLocationRequest(location: CLLocationCoordinate2D, identifier: NSString, completion: completionBlock) {
var error: NSError?
var response: NSURLResponse?
let queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)
dispatch_async(queue, {
if let data = NSURLConnection.sendSynchronousRequest(self.buildRequestForVenueLocation(location, identifier), returningResponse: &response, error: &error) {
let responseDictionary: AnyObject? = NSJSONSerialization.JSONObjectWithData(data, options: .allZeros, error:&error) ?? error?.localizedDescription
completion(venues: responseDictionary, error: error)
} else {
println("There was a problem with the request...")
}
})
}
and I implement it like this:
@IBAction func testJSON(sender: AnyObject) {
v.performVenueLocationRequest(l.location, identifier: "4d4b7105d754a06374d81259", completion: { dict, err in
dispatch_async(dispatch_get_main_queue(), {
if let dict = dict as? NSDictionary {
println(dict)
} else {
if let err = err {
println(err)
}
}
})
})
}
Now - This works, but being fairly new to this language it's not easy to spot things that is redundant or could be improved/changed
I'd love some input Amit Bijlani or Pasan Premaratne