NSError(domain: urlString, code: httpRes.statusCode, userInfo: nil):
//1.創(chuàng)建NSURL對象
let url = NSURL(string: urlString)
//2.創(chuàng)建NSURLRequest對象
let request = NSURLRequest(URL: url!)
//3.NSURLSession對象
let session = NSURLSession.sharedSession()
//4.task
let task = session.dataTaskWithRequest(request) { (data, response, error) in
if error != nil {
//下載失敗
self.delegate?.sessionDownloader(self, didFailWithError: error)
}else{
//服務(wù)器成功接收請求
let httpRes = response as! NSHTTPURLResponse
if httpRes.statusCode == 200 {
//成功返回?cái)?shù)據(jù)
self.delegate?.sessionDownloader(self, didFinishWithData: data)
}else{
//請求參數(shù)有錯誤
let error = NSError(domain: urlString, code: httpRes.statusCode, userInfo: nil)
self.delegate?.sessionDownloader(self, didFailWithError: error)
}
}
}