Skip to content

Instantly share code, notes, and snippets.

@ixqbar
Created June 3, 2015 07:10
Show Gist options
  • Save ixqbar/69206bc9892e71ae2530 to your computer and use it in GitHub Desktop.
Save ixqbar/69206bc9892e71ae2530 to your computer and use it in GitHub Desktop.
Swift synchronous request
class Request : NSObject {
func send(url: String, f: (String)-> ()) {
var request = NSURLRequest(URL: NSURL(string: url)!)
var response: NSURLResponse?
var error: NSErrorPointer = nil
var data = NSURLConnection.sendSynchronousRequest(request, returningResponse: &response, error: error)
var reply = NSString(data: data!, encoding: NSUTF8StringEncoding)
f(reply as! String)
}
}
println("start")
var request = Request()
request.send("http://linjujia.cn/", f : {(result: String)-> () in
println(result)
})
println("end")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment