Passing along values from 1 function to another in Swift

59 views Asked by At

So I've got 2 functions in this class that I'm working on that need to use the same variable. This variable is filled with a JSON object from the SwiftyJSON framework.

I start of instaniating the variable like so:

 var fooGame : JSON?;

Then I fill this bad boy up like this:

func tableView(gamesListTableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    if (indexPath.row < InvitesList.count) {
        //acceptInvite
    } else {
        performSegueWithIdentifier("presentGame", sender: self)
        self.fooGame = GamesList[indexPath.row - InvitesList.count];

        println(self.fooGame);
    }
}

That works perfect and the value of fooGame gets printed to the console! However this is where the problem occurs, I want to use the value in self.fooGame again here:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {
    if (segue.identifier == "presentGame") {
        var svc = segue.destinationViewController as! Game;

        println(self.fooGame);
    }
}

Here I get nil printed in the console.

1

There are 1 answers

0
Özgür Ersil On BEST ANSWER

change the line order these 2 lines, because you are setting your fooGame after you perform segue

self.fooGame = GamesList[indexPath.row - InvitesList.count];
performSegueWithIdentifier("presentGame", sender: self)