NSError: 小技巧

NSError(domain: urlString, code: httpRes.statusCode, userInfo: nil):

//1.创建NSURL对象
        let url = NSURL(string: urlString)
        //2.创建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{
                //服务器成功接收请求
                let httpRes = response as! NSHTTPURLResponse
                
                if httpRes.statusCode == 200 {
                    //成功返回数据
                    self.delegate?.sessionDownloader(self, didFinishWithData: data)
                }else{
                    //请求参数有错误
                    let error = NSError(domain: urlString, code: httpRes.statusCode, userInfo: nil)
                    self.delegate?.sessionDownloader(self, didFailWithError: error)
                }
                
                
            }
            
            
        }

你可能感兴趣的:(NSError: 小技巧)