Parsing JSON into swift predefined array -


i trying items api (json) , parse predefined swift array. have searched , looked hours due lack of skills wasn't able find suitable case.

my predefined array looks this:

init?(participants: string, photoguest: uiimage?, photohome: uiimage?, time: string, stadium: string, channel: string) 

the json structure this(entire json file):

{"gameid":"255","gameweek":"17","gamedate":"2016-01-03","awayteam":"sea","hometeam":"ari","gametimeet":"4:25 pm","tvstation":"fox","winner":"sea"} 

my current code looks (games class connect variables array table cell items):

var gameplan = [games]()  func loadnflgames(){      let apiurl = nsurl(string: "http://www.fantasyfootballnerd.com/service/schedule/json/test/")     let data: anyobject? = nsdata(contentsofurl: apiurl!)       let hometeam = (data as! nsdictionary)["hometeam"] as! string     let awayteam = (data as! nsdictionary)["awayteam"] as! string     let gamedate = (data as! nsdictionary)["gamedate"] as! string     let gametimeet = (data as! nsdictionary)["gametimeet"] as! string     let tvstation = (data as! nsdictionary)["tvstation"] as! string      /*     schleife mit api daten:         gameweek = currentweek{ //every game gameweek matches currentweek     */      // create variables api calls     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)!      // add data array     gameplan.append(gamedata) } 

i getting following error:

could not cast value of type '_nsinlinedata' (0x1a0cfd428) 'nsdictionary' (0x1a0cf3380).

edit: error being thrown here:

let hometeam = (data as! nsdictionary)["hometeam"] as! string 

your highly appreciated. in advance!

hello data variable doesn't contain json r looking for. have serialize json alexander suggested nsjsonserialization can throw error have tu put in in try code (i suggest using dispatch_async make in background thread use completion closure result)-->

    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)                     //here can loop through jsonobject data looking                     //when array of games pass the completion closure completionclosure(result: gameplan)                 }             }          })     } 

ps: please let me know if need more help.


Comments