Created
June 13, 2015 19:57
-
-
Save gavi/908edfd49a93df3ab123 to your computer and use it in GitHub Desktop.
Semaphore based NSURLSession for Command Line GeoCoder
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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