Skip to content

Instantly share code, notes, and snippets.

@gavi
Created June 13, 2015 19:57
Show Gist options
  • Save gavi/908edfd49a93df3ab123 to your computer and use it in GitHub Desktop.
Save gavi/908edfd49a93df3ab123 to your computer and use it in GitHub Desktop.
Semaphore based NSURLSession for Command Line GeoCoder
import Foundation
func BlockingGet(url:String)->String?{
let sema=dispatch_semaphore_create(0)
let url:NSURL!=NSURL(string:url)
var output:String?
NSURLSession.sharedSession().dataTaskWithURL(url) {
data, response, error in
output = NSString(data: data!, encoding: NSUTF8StringEncoding) as String?
dispatch_semaphore_signal(sema)
}!.resume()
dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER)
return output
}
let url="http://maps.googleapis.com/maps/api/geocode/json?address=1%20University%20Plaza,%2011021"
print(BlockingGet(url)!)
print("Done")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment