HTTPGet("http://www.baidu.com") {
(data: String,error: String?) -> Void in
if error != nil {
println(error)
} else {
println(data)
}
}
func HTTPGet(url: String,callback: (String,String?) -> Void) {
var request = NSMutableURLRequest(URL: NSURL(string: url)!)
HTTPsendRequest(request,callback)
}
func HTTPsendRequest(request: NSMutableURLRequest,
callback: (String,arial; font-size:14px; line-height:21px"> let task = NSURLSession.sharedSession().dataTaskWithRequest(
request,arial; font-size:14px; line-height:21px"> {
data,response,error in
if error != nil {
callback("",error.localizedDescription)
} else {
callback(
NSString(data: data,encoding: NSUTF8StringEncoding)!,arial; font-size:14px; line-height:21px"> nil
)
}
})
task.resume()
}
原文链接:https://www.f2er.com/swift/327258.html