gpt4 book ai didi

swift - 如何找出某个属性是否在用户的表中?

转载 作者:行者123 更新时间:2023-11-28 10:30:23 29 4
gpt4 key购买 nike

我有一个 firebase 表,我需要在其中找出特定用户的属性是否存在于表中。表的结构如下:

Users
-Lf9xUh53VeL4OLlwqQo
username: "my@test.com"
price: "$100"
special: "No"

-L12345ff322223345fd
username: "my2@test.com"
special: "No"

我需要查明是否为特定用户添加了“价格”。似乎无法弄清楚这一点!

很快我需要这样的东西:

        self.ref?.child("Users").queryOrdered(byChild: "username").queryEqual(toValue: username.text!).observe(.value, with: { (snapShot) in
if (snapShot.value! is NSNull) {
print("nothing found")
} else {
print("found it!")
print(snapShot)

let snapShotValue = snapShot.value as! [String:[String:Any]]
Array(snapShotValue.values).forEach { // error here if it doesn't exist
let price = $0["price"] as! String
self.userPrice.text = price
}}
})

但如果价格不存在,我就会收到错误消息。感谢您的帮助。

最佳答案

使用 as? 而不是 as!

if let price = $0["price"] as? String {
print(price)
}
else {
print("No price")
}

或不久

self.userPrice.text = ($0["price"] as? String) ?? "No price"

关于swift - 如何找出某个属性是否在用户的表中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56486253/

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