[英]Retrieving child values from Firebase
I have a query the looks for a specific UUID and returns the object. 我有一个查询,查找特定的UUID并返回对象。
print(snapshot)
Snap (users) {
"-KENROf9kGUPw1j6ghFo" = {
UUID = "A1166D66-31B4-4B1C-B28C-58E9E5615D16";
nickname = Craig;
};
}
I'm trying to extract the nickname
but having trouble. 我正在尝试提取
nickname
但遇到了麻烦。
Here is my query let queryRef = Firebase(url:"https://<url>.firebaseio.com/users")
这是我的查询,
let queryRef = Firebase(url:"https://<url>.firebaseio.com/users")
And here is the query reference. 这是查询参考。
var handle = queryRef.queryOrderedByChild("UUID").queryEqualToValue("A1166D66-31B4-4B1C-B28C-58E9E5615D16").observeSingleEventOfType(.Value, withBlock: { snapshot in
if snapshot.value is NSNull {
print("snapshot empty")
} else {
print(snapshot) //this prints the snapshot above
self.nickname = snapshot.value.objectForKey("nickname") as! String //this returns an error
print(self.nickname)
}
})
}
The self.nickname....
line returns the following error. self.nickname....
行返回以下错误。
fatal error: unexpectedly found nil while unwrapping an Optional value
I've tried a few different ways of structuring this, but have not yet found a way that doesn't fail. fatal error: unexpectedly found nil while unwrapping an Optional value
我尝试了几种不同的结构方式,但尚未找到不会失败的方法。
You're storing the users under a push id and then querying them by their uid. 您将用户存储在推送ID下,然后通过其uid查询用户。 That's a bad idea.
那是个坏主意。 Instead, store the users under their uid (or in your case UUID) as this sample from the Firebase documentation on storing user data shows:
而是将用户存储在其uid(或您的情况下为UUID)下,如Firebase文档中存储用户数据的示例所示:
ref.authUser("jenny@example.com", password:"correcthorsebatterystaple") {
error, authData in
if error != nil {
// Something went wrong. :(
} else {
// Authentication just completed successfully :)
// The logged in user's unique identifier
println(authData.uid)
// Create a new user dictionary accessing the user's info
// provided by the authData parameter
let newUser = [
"provider": authData.provider,
"displayName": authData.providerData["displayName"] as? NSString as? String
]
// Create a child path with a key set to the uid underneath the "users" node
// This creates a URL path like the following:
// - https://<YOUR-FIREBASE-APP>.firebaseio.com/users/<uid>
ref.childByAppendingPath("users")
.childByAppendingPath(authData.uid).setValue(newUser)
}
}
If you insist sticking to your current data structure, you'll need to handle the fact that a .Value
query returns a list of children. 如果您坚持使用当前数据结构,则需要处理
.Value
查询返回子级列表的事实。 Even if there's only one item that matches the query condition, it will still be a list of one item. 即使只有一项符合查询条件,它仍将是一项的列表。
var query = queryRef.queryOrderedByChild("UUID").queryEqualToValue("A1166D66-31B4-4B1C-B28C-58E9E5615D16")
query.observeSingleEventOfType(.Value, withBlock: { snapshot in
if snapshot.value is NSNull {
print("snapshot empty")
} else {
for child in snapshot.children {
self.nickname = child.value.objectForKey("nickname") as! String
print(self.nickname)
}
}
})
Replace this line: 替换此行:
self.nickname = snapshot.value.objectForKey("nickname") as! String
with the following: 具有以下内容:
self.nickname = snapshot.value["nickname"] as! String
And make sure that your snapshot is an object with a property "nickname" 并确保您的快照是具有“昵称”属性的对象
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.