Northfield82 Northfield82 - 22 days ago 6
Swift Question

Where am I going wrong with converting this code so it does not put the variable in an array?

This following snippet of code:

if let dict = json?["Stats"] as? [String:AnyObject] {

if let newdict = dict["biggestWin"] as? [[String : AnyObject]] {
self.bigWinPlayer1Goals = newdict.flatMap { $0["player1_goals"] as! String!}
print (self.bigWinPlayer1Goals)
}


outputs:


["1"]


However, that
1
is contained within an array - this I do not want. So I have re-written the code to this:

if let newdict = dict["biggestWin"] as? [String : AnyObject] {
let bigWinPlayer1Goals = newdict ["player1_goals"] as? String
print (bigWinPlayer1Goals!)
}


However this does not work, and does not output anything.

Where am I going wrong?

This is the array in question:

["Stats": {
biggestWin = (
{
"player1_goals" = 1;
"player2_goals" = 2;
}
);
goalDifference = "-17";
goalsConceded = 32;
goalsScored = 15;
totalDraws = 3;
totalLosses = 10;
totalWins = 1;
winPercentage = "7.14%";
}]


I would like to be able to store
player1_goals
and
player2_goals
in two separate variables.

Answer
if let data = dict["biggestWin"] as? [[String : AnyObject]] {
    let newdict = data.first!
    let bigWinPlayer1Goals = newdict ["player1_goals"] as? String
    print (bigWinPlayer1Goals!)
}
Comments