gpt4 book ai didi

swift - 如何从 firebase (Swift) 检索数据

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

我是初学者,我想使用 swift 作为我的编程语言。

用户可以添加注册用户并互相转账,因此当用户输入照片号码和电子邮件时,应用程序可以检查输入的号码是否已在 Firebase 系统中注册

任何简单的方法来搜索用户是否存在,非常感谢

这是数据库的结构

enter image description here

var ref: DatabaseReference!
var tref: DatabaseReference!

var handle : DatabaseHandle!

var usersArray = [NSDictionary?]()
var filteredUsers = [NSDictionary?]()
var user : NSDictionary?

override func viewDidLoad() {
super.viewDidLoad()
self.picker.isHidden = true
tref = Database.database().reference()
ref = Database.database().reference()
self.handle = self.ref?.child("users").child((Auth.auth().currentUser?.uid)!).child("contact").observe(.childAdded, with: { (snapshot) in
if let item = snapshot.value as? String {
self.pickerdata.append(item)
self.picker.reloadAllComponents()
}
})
self.picker.delegate = self
self.picker.dataSource = self

tref.child("users").queryOrdered(byChild: "phone").observe(.childAdded, with: {(snapshot) in
self.usersArray.append(snapshot.value as? NSDictionary)
})
print(usersArray.count)

}

@IBAction func ContactChange(_ sender: UITextField) {
filteredContent(searchText: contactText.text!)
print(filteredUsers.count)
print(usersArray.count)
print("ARRAY")

}

func filteredContent(searchText: String){
self.filteredUsers = self.usersArray.filter{ user in
let username = user!["phone"] as? String
return (username?.lowercased().contains(searchText.lowercased()))!
}
}

func findUsers(text: String){
self.handle = ref.child("users").queryOrdered(byChild: "phone").queryStarting(atValue: contactText.text!).queryEnding(atValue: contactText.text!+"\u{f8ff}").observe(.value, with: { snapshot in
if let item = snapshot.value as? String {
self.contact.append(item)
} else{
print("error")
}
})
}

上面的代码没有多大作用。非常感谢您的帮助

最佳答案

这个问题有点不清楚,但我认为OP要问的是:

how to see if a user exists by their phone number?

如果这是所要求的,则可以通过查询该节点的子节点来检索该节点。例如,让我们在 users 节点上创建一个查询来查找包含名称的子节点:Kam querying by phone

let usersRef = firebaseRef.child("users")
let query = usersRef.queryOrdered(byChild: "phone").queryEqual(toValue: "2330504")
query.observeSingleEvent(of: .value) { (snapshot) in
if snapshot.exists() {
//need to iterate in case we find more than one match
for child in snapshot.children {
let snap = child as! DataSnapshot
let dict = snap.value as! [String: Any]
let name = dict["name"] as! String
print("found \(name)")
}

} else {
print("not found")
}
}

运行此代码时会打印

found Kam

假设电话号码是2330504

关于swift - 如何从 firebase (Swift) 检索数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47166445/

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