gpt4 book ai didi

swift - 使用 Swift 在 Firebase 中获取 Children of Children

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

我有一个简单的 firebase 数据库,我需要从中获取子值。如何获取子字段值?这是我的数据库:

Users

-LddrOHGdBf_GcPrcvYn

FirstName: Mike

LastName: Smith

username: m@m.com

我一直在研究堆栈溢出,似乎没有什么可以回答这个问题,但我知道这一定很容易。

import UIKit
import FirebaseDatabase

class CustProfileViewController: UIViewController {
var ref: DatabaseReference?
var tempName = ""
var tempFirstName = ""
@IBOutlet weak var username: UITextField!
@IBOutlet weak var firstName: UITextField!
@IBOutlet weak var editAddressButton: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
ref = Database.database().reference()
username.text = "m@m.com"
}

@IBAction func editAddressButtonPressed(_ sender: Any) {
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! NSDictionary
let fName = snapShotValue["FirstName"] as? String
self.firstName.text = fName
print(self.firstName.text!)

}
})
}

}

我期望用 fName 填充firstName 文本框,但它返回一个 nil 值。

最佳答案

你可以试试

@IBAction func editAddressButtonPressed(_ sender: Any) {
self.ref?.child("Users").queryOrdered(byChild: "username").queryEqual(toValue: username.text!).observe(.value, with: { (snapShot) in
if !snapShot.exists() {
print("nothing found")
} else {
print("found it!")
print(snapShot)
let snapShotValue = snapShot.value as! [String:[String:Any]]
Array(snapShotValue.values).forEach {
let fName = $0["FirstName"] as! String
print(fName)
}

}
})
}

关于swift - 使用 Swift 在 Firebase 中获取 Children of Children,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56138102/

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