NSError: 팁

1176 단어
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)
                }
                
                
            }
            
            
        }

좋은 웹페이지 즐겨찾기