使用Swift在Firebase中让孩子们的孩子 [英] Get Children of Children in Firebase Using Swift
本文介绍了使用Swift在Firebase中让孩子们的孩子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个简单的firebase数据库,我需要从中获取子级值.如何获取子级字段值?这是我的数据库:
I have a simply firebase database that I need to get the children values from. How can I obtain the children field values? Here is my database:
Users
-LddrOHGdBf_GcPrcvYn
FirstName: Mike
LastName: Smith
username: m@m.com
我到处都是堆栈溢出,似乎没有人回答这个问题,但是我知道这一定很容易.
I have been all over stack overflow and nothing seems to answer this question, but I know it must be easy.
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!)
}
})
}
}
我希望firstName文本框填充fName,但它返回的是nil值.
I am expecting the firstName text box to be populated with the fName, but it is returning a nil value.
推荐答案
您可以尝试
@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在Firebase中让孩子们的孩子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文