這個代碼下面的代碼片斷:我在哪裏轉換此代碼出錯了,所以它不會把變量放在數組中?
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)
}
輸出:
[ 「1」]
然而,1
被包含在陣列中 - 這我不想要的。所以我已將代碼重新寫入:
if let newdict = dict["biggestWin"] as? [String : AnyObject] {
let bigWinPlayer1Goals = newdict ["player1_goals"] as? String
print (bigWinPlayer1Goals!)
}
但是,這不起作用,並且不輸出任何內容。
我哪裏錯了?
這是有問題的數組:
["Stats": {
biggestWin = (
{
"player1_goals" = 1;
"player2_goals" = 2;
}
);
goalDifference = "-17";
goalsConceded = 32;
goalsScored = 15;
totalDraws = 3;
totalLosses = 10;
totalWins = 1;
winPercentage = "7.14%";
}]
我希望能夠存儲player1_goals
和player2_goals
在兩個獨立的變量。
請將使用過的數據和您的預期輸出看起來像 – shallowThought