swift 自带HTTP请求

前端之家收集整理的这篇文章主要介绍了swift 自带HTTP请求前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

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()

}

猜你在找的Swift相关文章