Cappuccino90

Untitled

Jul 22nd, 2016
257
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  class JSONParserClass {
  2.         func parseResponse(data: NSData?, response:
  3.             NSURLResponse?,error:
  4.             NSError?,completionHandler: (parsedData:
  5.             [Repository],error: NSError?)  -> ()) throws
  6.         {
  7.             guard let data = data else {
  8.                 throw JSONError.NoData
  9.             }
  10.            
  11.             guard let json = try NSJSONSerialization.JSONObjectWithData(data, options:.AllowFragments) as? [[String: AnyObject]] else {
  12.                 throw JSONError.ConversionFailed
  13.             }
  14.            
  15.             var jsonRepos:[Repository] = Array<Repository>()
  16.            
  17.            
  18.             for a in json {
  19.                 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)!)
  20.                 jsonRepos.append(temp)
  21.             }
  22.             completionHandler(parsedData: jsonRepos, error: nil)
  23.         }
  24.     }
  25.    
  26.     func jsonParser() {
  27.         let queue = NSOperationQueue()
  28.         let o = JSONParserClass()
  29.        
  30.        
  31.         let blockOperation:NSBlockOperation = NSBlockOperation( block: {
  32.             let urlPath = "https://api.github.com/users/\(self.githubName)/repos"
  33.            
  34.             guard let endpoint = NSURL(string: urlPath) else {
  35.                 let alert = UIAlertController(title: "Error occured when trying to connect to a ressource.", message: "Error creating endpoint", preferredStyle: UIAlertControllerStyle.Alert)
  36.                 let okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.Default) {
  37.                     UIAlertAction in
  38.                     self.navigationController?.popToRootViewControllerAnimated(true)
  39.                 }
  40.                 alert.addAction(okAction)
  41.                 self.presentViewController(alert, animated: true, completion: nil);
  42.                
  43.                 return
  44.             }
  45.             let request = NSMutableURLRequest(URL:endpoint)
  46.            
  47.             NSURLSession.sharedSession().dataTaskWithRequest(request) { (data, response, error) in
  48.                 do {
  49.                    
  50.                    
  51.                     try o.parseResponse(data, response: response, error: error,  completionHandler:  { (parsedData, error) in
  52.                         self.allRepos.removeAll()
  53.                        
  54.                         for p in parsedData {
  55.                             self.allRepos.append(p)
  56.                         }
  57.                        
  58.                         self.tableRefresh(self.allRepos)
  59.                     })
  60.                 } catch let error as JSONError {
  61.                     let alert = UIAlertController(title: "Error Github Name Request", message: "\(error.rawValue)", preferredStyle: UIAlertControllerStyle.Alert)
  62.                    
  63.                     let okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.Default) {
  64.                         UIAlertAction in
  65.                         self.navigationController?.popToRootViewControllerAnimated(true)
  66.                     }
  67.                    
  68.                     alert.addAction(okAction)
  69.                     self.presentViewController(alert, animated: true, completion: nil);
  70.                    
  71.                 } catch let error as NSError {
  72.                     let alert = UIAlertController(title: "Error occurred", message: "\(error.description)", preferredStyle: UIAlertControllerStyle.Alert)
  73.                    
  74.                     let okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.Default) {
  75.                         UIAlertAction in
  76.                         self.navigationController?.popToRootViewControllerAnimated(true)
  77.                     }
  78.                     alert.addAction(okAction)
  79.                     self.presentViewController(alert, animated: true, completion: nil);
  80.                 }
  81.                 }.resume()
  82.         })
  83.         queue.addOperation(blockOperation)
  84.        
  85.     }
Advertisement