【发布时间】:2016-07-01 01:31:40
【问题描述】:
我有一个搜索栏,当您键入它时,它会在数据库中搜索该用户的用户名。找到用户名的代码有效,但是当我尝试读取特定值时它崩溃了。这是代码
func searchBar(searchBar: UISearchBar, textDidChange searchText: String) {
Database.child("users").queryOrderedByChild("username").queryEqualToValue(searchText).observeSingleEventOfType(.Value, withBlock: { snapshot in
print(snapshot)
print(snapshot.value!["first_name"] as? String)
print(snapshot.value!["last_name"] as? String)
print(snapshot.value!["username"] as? String)
print(snapshot.value!["profile_picture_url"] as? String)
})
}
打印快照的结果是
Snap (users) {
12345UIDEXample = {
"first_name" = Bob;
"last_name" = Someone;
"profile_picture_url" = "exampleurl.com";
username = bobby;
};
}
但是当我尝试访问时
snapshot.value!["first_name"] as? String
它返回 nil 并崩溃? 为什么如果它在 json 中清楚地显示它返回数据存在但不允许我提取值?
【问题讨论】:
标签: swift firebase firebase-realtime-database