swift - Swift firebase 我的数据库有点复杂我应该如何访问这个孩子?
问题描述
我使用 firebase 作为我的数据库。所以我的数据库变得复杂了,我被困在访问一个特定的孩子上。
我有这个片段:
for child in snapshot.childSnapshot(forPath: "rated").children{
let child = child as! DataSnapshot
但在那之后我不知道该怎么办。
我的 json 结构如下所示:
-LfdhZk3UY9ag5zIYUgh
rated
2fLHYArJzCR0wg6waeW23n859pI3
-Lff-x5TTeOn7ZO1ygDc
UserId:"05E1OFKYsiWQf2s7LsEm6XM4oWy1"
-Lff0DsKvs5lnxtFCQL_
UserId:"Q6jYe9gzAePy8x6SJulTYLMQTpN2"
我想访问 2fLHYArJzCR0wg6waeW23n859pI3 下的所有 UserId。但我一直都是零,现在我迷路了
解决方案
在rated
您的下方似乎有两个动态级别,您通常会通过遍历子级来处理它们。然后您可以再次访问属性 ( UserId
) childSnapshot(forPath:)
。
所以像:
for child in snapshot.childSnapshot(forPath: "rated").children{
for userSnapshot in child.children.allObjects as! [DataSnapshot] {
for dynamicSnapshot in userSnapshot.children.allObjects as! [DataSnapshot] {
let userID = dynamicSnapshot.childSnapshot(forPath: "UserId").value
推荐阅读
- angular - 我应该为与 RxJS 的简单交互编写一个单元测试吗
- python-3.x - 我的 Python 获取请求中是否有 Eclipse Pydev 设置或某些东西阻止我与主机连接?
- node.js - 为什么本机 NodeJS 的 setInterval() 函数在可能非常准确时却如此不准确?
- ios - View 的 Frame 被调用两次并导致剪辑错误
- mysql - 如何使用sql从数据库中的日期列得出年龄?
- python - 如何使用 selenium 选择性地遍历下拉菜单
- regex - 如何防止正则表达式匹配空路径?
- gradle - 使用 Junit5 进行 Gradle 多项目集成测试
- r - 如何查找任何列是否具有我正在寻找的特定值?
- java - 如何使用 Spring Boot 在运行时创建指向系统中 REST 端点的 URL?