Cappuccino90 icon

Untitled

Cappuccino90 | PRO | 07/22/16 06:58:25 PM UTC | 0 ⭐ | 7630 👁️ | Never ⏰ | []
Objective C |

0 B

|

None

|

0 👍

/

0 👎

 class JSONParserClass {
        func parseResponse(data: NSData?, response:
            NSURLResponse?,error:
            NSError?,completionHandler: (parsedData:
            [Repository],error: NSError?)  -> ()) throws
        {
            guard let data = data else {
                throw JSONError.NoData
            }
            
            guard let json = try NSJSONSerialization.JSONObjectWithData(data, options:.AllowFragments) as? [[String: AnyObject]] else {
                throw JSONError.ConversionFailed
            }
            
            var jsonRepos:[Repository] = Array<Repository>()
            
            
            for a in json {
                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)!)
                jsonRepos.append(temp)
            }
            completionHandler(parsedData: jsonRepos, error: nil)
        }
    }
    
    func jsonParser() {
        let queue = NSOperationQueue()
        let o = JSONParserClass()
        
        
        let blockOperation:NSBlockOperation = NSBlockOperation( block: {
            let urlPath = "https://api.github.com/users/\(self.githubName)/repos"
            
            guard let endpoint = NSURL(string: urlPath) else {
                let alert = UIAlertController(title: "Error occured when trying to connect to a ressource.", message: "Error creating endpoint", preferredStyle: UIAlertControllerStyle.Alert)
                let okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.Default) {
                    UIAlertAction in
                    self.navigationController?.popToRootViewControllerAnimated(true)
                }
                alert.addAction(okAction)
                self.presentViewController(alert, animated: true, completion: nil);
                
                return
            }
            let request = NSMutableURLRequest(URL:endpoint)
            
            NSURLSession.sharedSession().dataTaskWithRequest(request) { (data, response, error) in
                do {
                    
                    
                    try o.parseResponse(data, response: response, error: error,  completionHandler:  { (parsedData, error) in
                        self.allRepos.removeAll()
                        
                        for p in parsedData {
                            self.allRepos.append(p)
                        }
                        
                        self.tableRefresh(self.allRepos)
                    })
                } catch let error as JSONError {
                    let alert = UIAlertController(title: "Error Github Name Request", message: "\(error.rawValue)", preferredStyle: UIAlertControllerStyle.Alert)
                    
                    let okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.Default) {
                        UIAlertAction in
                        self.navigationController?.popToRootViewControllerAnimated(true)
                    }
                    
                    alert.addAction(okAction)
                    self.presentViewController(alert, animated: true, completion: nil);
                    
                } catch let error as NSError {
                    let alert = UIAlertController(title: "Error occurred", message: "\(error.description)", preferredStyle: UIAlertControllerStyle.Alert)
                    
                    let okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.Default) {
                        UIAlertAction in
                        self.navigationController?.popToRootViewControllerAnimated(true)
                    }
                    alert.addAction(okAction)
                    self.presentViewController(alert, animated: true, completion: nil);
                }
                }.resume()
        })
        queue.addOperation(blockOperation)
        
    }

Comments

  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎

    
        
  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎

    
        
  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎