【发布时间】:2020-05-09 00:40:18
【问题描述】:
我有这个数据结构,我无法提取正确的值:
users
private
userID
birthday: "birthdayValue"
username: "nathan"
firstName: "Nathan"
etc...
我正在我的应用中创建一个搜索功能,通过用户名通过 firebase 实时数据库搜索用户:
let reference = Database.database().reference()
if(searchText != ""){
reference.child("users").child("private").queryOrdered(byChild: "username").queryStarting(atValue: searchText).queryEnding(atValue: searchText + "\u{f8ff}").observeSingleEvent(of: .value, with: { (snapshot) in
if snapshot.value is NSNull{
//handles errors
return
}
else{
if let user = snapshot.value as? NSDictionary {
for child in user{
print(child.key)
print(child.value)
}
}
else{
//null
}
}
})
目前,每次搜索时,这两个打印语句都会在控制台中打印这两个结果:
wnszfmHilqNl6PG9khWtWkKUPtF3
{
birthday = 100;
dateCreated = "1579543450313.94";
description = nil;
email = "email@email.com";
firstName = Nathan;
instagramLink = nil;
lastLogin = "1579543450313.988";
lastName = Ellis;
profilePicURL = "url";
twitchLink = nil;
username = nathan;
youtubeLink = nil;
}
这是预期的,它将用户 ID(键)和快照的值打印为 NSDictonary。我只对获取用户名感兴趣,没有别的。我如何从这个 firebase 快照中提取用户名,以便我可以将他们的用户名作为字符串添加到我的搜索控制器的数组中?
显然它需要是动态的,因为用户 ID 总是不同的。
我需要更改我的数据模型吗?
【问题讨论】:
-
你的数据模型到底是什么?您是否使用符合
Codable的结构? -
我没有使用数据模型,我只是想从收到的快照中获取用户名并将其添加到一个简单的数组中
标签: ios swift firebase firebase-realtime-database