gpt4 book ai didi

json - 在 JSONObject Swift 中检查键

转载 作者:行者123 更新时间:2023-11-28 08:14:53 25 4
gpt4 key购买 nike

我是 Swift 的新手。如何检查来自 JSONObject 的 key 是否存在?在 Java 中,我可以这样做 jsonObject.has("selected") 。在我的代码下面:

currTable = filteredDataTable[indexPath.row] as! [AnyHashable: Any]

if ((currTable.index(forKey: "selected") != nil) /*currTable["selected"]) != nil*/) {
currTable.updateValue((!((currTable["selected"] as? Bool)!)), forKey: "selected")
}
else {
currTable.updateValue(true, forKey: "selected")
}
print(currTable)

在我上面的代码中,当我单击 CollectionView Cell 时,它总是给我 else 条件。因此,我的 JSONObject 中的 selected 键始终为真。我做错了吗?我读过this但它并不清楚我所有的问题。

最佳答案

试试这个:

currTable = filteredDataTable[indexPath.row] as! [AnyObject: Any]

if let selectedTableElement = currTable["selected"] != nil {
currTable.updateValue(false, forKey: "selected")
}
else {
currTable.updateValue(true, forKey: "selected")
}
print(currTable)

关于json - 在 JSONObject Swift 中检查键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42848977/

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