Cappuccino90

Untitled

Jul 21st, 2016
159
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.86 KB | None | 0 0
  1.     func jsonParser() {
  2.         let urlPath = "https://api.github.com/users/\(githubName)/repos"
  3.        
  4.         guard let endpoint = NSURL(string: urlPath) else {
  5.             print("Error creating endpoint")
  6.             let alert = UIAlertController(title: "Error Github Name Request", message: "Error creating endpoint", preferredStyle: UIAlertControllerStyle.Alert)
  7.             let okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.Default) {
  8.                 UIAlertAction in
  9.                 self.navigationController?.popToRootViewControllerAnimated(true)
  10.             }
  11.             alert.addAction(okAction)
  12.             self.presentViewController(alert, animated: true, completion: nil);
  13.            
  14.             return
  15.         }
  16.        
  17.        
  18.        // How would I implement this part of the code, to make the response asynchronous?
  19.    
  20.         let queue = NSOperationQueue()
  21.         let o = op()
  22.        
  23.         queue.addOperation(o.parseResponse(data: NSData?, response: NSURLResponse?, error: NSError?, completionHandler: (parsedData: [ViewControllerTableView.Repository], error: NSError) -> ()))
  24.        
  25.  
  26.  
  27.  
  28.  
  29.         let request = NSMutableURLRequest(URL:endpoint)
  30.         NSURLSession.sharedSession().dataTaskWithRequest(request) { (data, response, error) in
  31.             do {
  32.                 guard let data = data else {
  33.                     let alert = UIAlertController(title: "Error Github Name Request", message: "\(JSONError.NoData)", preferredStyle: UIAlertControllerStyle.Alert)
  34.                    
  35.                     let okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.Default) {
  36.                         UIAlertAction in
  37.                         self.navigationController?.popToRootViewControllerAnimated(true)
  38.                     }
  39.                    
  40.                     alert.addAction(okAction)
  41.                     self.presentViewController(alert, animated: true, completion: nil);
  42.                     throw JSONError.NoData
  43.                 }
  44.                
  45.                 guard let json = try NSJSONSerialization.JSONObjectWithData(data, options:.AllowFragments) as? [[String: AnyObject]] else {
  46.                    
  47.                     let alert = UIAlertController(title: "Error Github Name Request", message: "\(JSONError.ConversionFailed)", preferredStyle: UIAlertControllerStyle.Alert)
  48.                    
  49.                     let okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.Default) {
  50.                         UIAlertAction in
  51.                         self.navigationController?.popToRootViewControllerAnimated(true)
  52.                     }
  53.                     alert.addAction(okAction)
  54.                     self.presentViewController(alert, animated: true, completion: nil);
  55.                     throw JSONError.ConversionFailed
  56.                 }
  57.                
  58.                 self.owner=json[0]["owner"]!["login"]! as! String
  59.                 self.allRepos.removeAll()
  60.                 for a in json {
  61.                     let temp:Repository = Repository(id: (a["id"] as? Int)!,name: (a["name"] as? String), size: (a["size"] as? Int)!, watchers: (a["watchers"] as? Int), created_at: (a["created_at"] as? String)!, descr: (a["description"] as? String)!)
  62.                     self.allRepos.append(temp)
  63.                 }
  64.                 self.tableRefresh(self.allRepos)
  65.                
  66.             } catch let error as JSONError {
  67.                 print(error.rawValue)
  68.             } catch let error as NSError {
  69.                 print(error.debugDescription)
  70.             }
  71.             }.resume()
  72.  
  73.     }
  74.    
  75.    
  76.    
  77.     class op: NSBlockOperation {
  78.  
  79.         func parseResponse(data: NSData?, response:
  80.             NSURLResponse?,error:
  81.             NSError?,completionHandler: (parsedData:
  82.             [Repository],error: NSError) -> ())
  83.         {
  84.            
  85.  
  86.         }
  87.     }
Advertisement
Comments
  • User was banned
Add Comment
Please, Sign In to add comment