Alan Tingey
Alan Tingey

Reputation: 961

Populate UITableView from JSON Array using Swift

Dear stackoverflow community,

I have the following function written in swift:

func jsonParsing()
{


    let url2 = NSURL(string: "http://localhost:8000/straightred/jsonfixture")

     let data = NSData(contentsOfURL: url2!)

     var arr = NSJSONSerialization.JSONObjectWithData(data!, options: nil, error: nil) as NSArray

    for var i = 0 ; i < (arr as NSArray).count ; i++
    {
        arrDict.addObject((arr as NSArray) .objectAtIndex(i))
    }

    println(arrDict);

}

Which produces the following output:

(
        {
        fields =         {
            "away_team" = Burnley;
            awayteamscore = 2;
            fixturedate = "2014-11-22T15:00:00";
            fixturematchday = 12;
            fixturestatus = FINISHED;
            "home_team" = Stoke;
            hometeamscore = 1;
            soccerseason = 354;
        };
        model = "straightred.straightredfixture";
        pk = 136932;
    },
        {
        fields =         {
            "away_team" = Swans;
            awayteamscore = 1;
            fixturedate = "2014-11-22T15:00:00";
            fixturematchday = 12;
            fixturestatus = FINISHED;
            "home_team" = ManCity;
            hometeamscore = 2;
            soccerseason = 354;
        };
        model = "straightred.straightredfixture";
        pk = 136930;
    },
        {
        fields =         {
            "away_team" = Sunderland;
            awayteamscore = 0;
            fixturedate = "2014-11-22T15:00:00";
            fixturematchday = 12;
            fixturestatus = FINISHED;
            "home_team" = Foxes;
            hometeamscore = 0;
            soccerseason = 354;
        };
        model = "straightred.straightredfixture";
        pk = 137852;
    },
        {
        fields =         {
            "away_team" = "West Ham";
            awayteamscore = 1;
            fixturedate = "2014-11-22T15:00:00";
            fixturematchday = 12;
            fixturestatus = FINISHED;
            "home_team" = Everton;
            hometeamscore = 2;
            soccerseason = 354;
        };
        model = "straightred.straightredfixture";
        pk = 136929;
    },
        {
        fields =         {
            "away_team" = "West Bromwich";
            awayteamscore = 0;
            fixturedate = "2014-11-22T15:00:00";
            fixturematchday = 12;
            fixturestatus = FINISHED;
            "home_team" = Chelsea;
            hometeamscore = 2;
            soccerseason = 354;
        };
        model = "straightred.straightredfixture";
        pk = 136928;
    },
        {
        fields =         {
            "away_team" = QPR;
            awayteamscore = 0;
            fixturedate = "2014-11-22T15:00:00";
            fixturematchday = 12;
            fixturestatus = FINISHED;
            "home_team" = Newcastle;
            hometeamscore = 1;
            soccerseason = 354;
        };
        model = "straightred.straightredfixture";
        pk = 136931;
    },
        {
        fields =         {
            "away_team" = ManU;
            awayteamscore = 2;
            fixturedate = "2014-11-22T17:30:00";
            fixturematchday = 12;
            fixturestatus = FINISHED;
            "home_team" = Arsenal;
            hometeamscore = 1;
            soccerseason = 354;
        };
        model = "straightred.straightredfixture";
        pk = 136927;
    },
        {
        fields =         {
            "away_team" = Liverpool;
            awayteamscore = 1;
            fixturedate = "2014-11-23T13:30:00";
            fixturematchday = 12;
            fixturestatus = FINISHED;
            "home_team" = Crystal;
            hometeamscore = 3;
            soccerseason = 354;
        };
        model = "straightred.straightredfixture";
        pk = 136926;
    },
        {
        fields =         {
            "away_team" = Spurs;
            awayteamscore = 2;
            fixturedate = "2014-11-23T16:00:00";
            fixturematchday = 12;
            fixturestatus = FINISHED;
            "home_team" = Hull;
            hometeamscore = 1;
            soccerseason = 354;
        };
        model = "straightred.straightredfixture";
        pk = 136925;
    },
        {
        fields =         {
            "away_team" = Southampton;
            awayteamscore = 1;
            fixturedate = "2014-11-24T20:00:00";
            fixturematchday = 12;
            fixturestatus = FINISHED;
            "home_team" = "Aston Villa";
            hometeamscore = 1;
            soccerseason = 354;
        };
        model = "straightred.straightredfixture";
        pk = 136924;
    }
)

I then want to populate a UITableView with this information. I have the following two functions to achieve this:

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

    return arrDict.count
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell     {

    var cell:TblCell = self.tableView.dequeueReusableCellWithIdentifier("cell") as TblCell

    var homeTeamName : NSString=arrDict[indexPath.row] .valueForKey("home_team") as NSString

    var awayTeamName : NSString=arrDict[indexPath.row] .valueForKey("away_team") as NSString

    cell.lblHomeTeam.text = homeTeamName

    cell.lblAwayTeam.text = awayTeamName

    return cell

}

However, the output window shows:

fatal error: unexpectedly found nil while unwrapping an Optional value
(lldb) 

It highlights the "var homeTeamName : NSString=arrDict[indexPath.row] .valueForKey("home_team") as NSString" in green and states "EXC_BAD_INSTRUCTION".

I am convinced that I am trying to access the array incorrectly but I am not sure the correct way forward. Any help on this would be appreciated.

Many thanks, ALAN.

Upvotes: 1

Views: 1115

Answers (1)

Eric Aya
Eric Aya

Reputation: 70098

Look at your data: "home_team" and "away_team" are values from a level 2 dictionary, in the "fields" key of a level 1 dictionary.

But you try to access "home_team" and "away_team" directly inside your "arrDict" array of level 1 dictionaries.

Array => Dictionaries => Fields key of each dic => your data (in a dictionary)

So you should first access "fields":

let fields = arrDict[indexPath.row].valueForKey("fields") as NSDictionary

Then access the team:

let homeTeamName = fields.valueForKey("home_team") as NSString

Upvotes: 4

Related Questions