gpt4 book ai didi

ios - 转换此代码时我哪里出错了,因此它不会将变量放入数组中?

转载 作者:搜寻专家 更新时间:2023-11-01 07:18:22 26 4
gpt4 key购买 nike

以下代码片段:

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_goalsplayer2_goals 存储在两个单独的变量中。

最佳答案

dict["biggestWin"] 返回的值是 [String : AnyObject]] 类型,即 AnyObjectArray .

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

关于ios - 转换此代码时我哪里出错了,因此它不会将变量放入数组中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40563726/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com