i got working far. when building project , running function parses api content , crashes. here code:
// // gamestableviewcontroller.swift // football life // // created david seyboth on 18/01/16. // copyright © 2016 david seyboth. rights reserved. // import uikit class gamestableviewcontroller: uitableviewcontroller { // mark: properties var gameplan = [games]() override func viewdidload() { super.viewdidload() //load nfl games loadnflgames { (result) -> () in self.gameplan = result dispatch_async(dispatch_get_main_queue(),{ self.tableview.reloaddata() }) } } func loadnflgames(completionclosure: (result : [games]) ->()){ let queue: dispatch_queue_t = dispatch_get_global_queue(dispatch_queue_priority_default, 0) dispatch_async(queue, { let url = "http://www.fantasyfootballnerd.com/service/schedule/json/test/" print(url) if let data = nsdata(contentsofurl: nsurl(string: url)!){ if let jsonobject = try? nsjsonserialization.jsonobjectwithdata(data, options: nsjsonreadingoptions.mutablecontainers) as! nsmutabledictionary{ print(jsonobject) let hometeam = jsonobject["hometeam"] as! string let awayteam = jsonobject["awayteam"] as! string let gamedate = jsonobject["gamedate"] as! string let gametimeet = jsonobject["gametimeet"] as! string let tvstation = jsonobject["tvstation"] as! string let api_guest = awayteam let api_home = hometeam let api_tvhost = tvstation let api_time = gamedate + ", " + gametimeet + " et" // convert gamedate day e.g. sun let api_stadion = "n/a" // prepare data array let gamedata = games(participants: api_guest+" @ "+api_home, photoguest: uiimage(named: api_guest), photohome: uiimage(named: api_home), time: api_time, stadium: api_stadion, channel: api_tvhost)! self.gameplan.append(gamedata) completionclosure(result: self.gameplan) } } }) } override func didreceivememorywarning() { super.didreceivememorywarning() // dispose of resources can recreated. } // mark: - table view data source override func numberofsectionsintableview(tableview: uitableview) -> int { // #warning incomplete implementation, return number of sections return 1 } override func tableview(tableview: uitableview, numberofrowsinsection section: int) -> int { // #warning incomplete implementation, return number of rows return gameplan.count } override func tableview(tableview: uitableview, cellforrowatindexpath indexpath: nsindexpath) -> uitableviewcell { let cellidentifier = "gamesprototypecell" let cell = tableview.dequeuereusablecellwithidentifier(cellidentifier, forindexpath: indexpath) as! gamestableviewcell // fetches appropriate meal data source layout. let game = gameplan[indexpath.row] cell.participants_label.text = game.participants cell.photoguest_image.image = game.photoguest cell.photohome_image.image = game.photohome cell.time_label.text = game.time cell.stadium_label.text = game.stadium cell.channel_label.text = game.channel return cell } }
the crash @ line: let hometeam = jsonobject["hometeam"] as! string message:
fatal error: unexpectedly found nil while unwrapping optional value
you missing json format. values looking in array "schedule"
so code
if let jsonobject = try? nsjsonserialization.jsonobjectwithdata(data, options: nsjsonreadingoptions.mutablecontainers) as! nsmutabledictionary{ //this array of games let items = jsonobject["schedule"] as! nsmutablearray print(items) //loop in items , add values array item in items { let hometeam = item["hometeam"] as! string let awayteam = item["awayteam"] as! string let gamedate = item["gamedate"] as! string let gametimeet = item["gametimeet"] as! string let tvstation = item["tvstation"] as! string let api_guest = awayteam let api_home = hometeam let api_tvhost = tvstation let api_time = gamedate + ", " + gametimeet + " et" // convert gamedate day e.g. sun let api_stadion = "n/a" // prepare data array let gamedata = games(participants: api_guest+" @ "+api_home, photoguest: uiimage(named: api_guest), photohome: uiimage(named: api_home), time: api_time, stadium: api_stadion, channel: api_tvhost)! self.gameplan.append(gamedata) } }
Comments
Post a Comment