【问题标题】:How to call a field within a field Firebase Could DB using Swift [closed]如何使用 Swift 调用字段 Firebase 中的字段可以 DB [关闭]
【发布时间】:2020-04-17 12:25:17
【问题描述】:

我正在从我的 Firebase 数据库中获取信息,其工作方式如下:

let docRef = db.collection("users").document(AccessToken.current!.userID)
docRef.getDocument { (document, error) in
   if let document = document, document.exists {

    let dataDescription = document.data().map(String.init(describing:)) ?? "nil"
    print("name: \(dataDescription)")
    print(document["name"])
    print(document["ProfilePicURL"])
   } else {
       print("Document does not exist")
   }
}

但我的数据库看起来像:

]1

我有什么简单的方法可以访问它吗?

【问题讨论】:

  • 你分享的代码有什么问题?
  • 对不起,我更新了数据库以更好地反映问题。所以我可以使用上面的代码访问 name: 和 profilePicURL,在 profilePicURL 中我可以整体接收“数据”,但数据的第一个字段是高度。所以问题是我将如何访问它,我需要重新循环还是可以访问字段中的字段?
  • 当你请求一个作为对象的字段时,你会得到一个字典。然后,您可以像处理 Swift 中的任何其他字典一样处理其中的任何子字段。

标签: swift firebase google-cloud-firestore


【解决方案1】:

当您从文档快照中获得一个作为对象的字段时,您会得到一个字典。然后,您可以像在 Swift 中处理任何其他 Dictionary 一样处理其中的任何子字段。

比如:

print(document["ProfilePicURL"]["data"]["height"])

【讨论】:

  • 谢谢,这更有意义谢谢,类型 document["ProfilePicURL"] 以 Any 形式返回,所以如果我在上面尝试,我会收到错误 'Value of type 'Any?'没有下标'
  • 搜索该错误消息以了解如何将其再次转换为 Dictionary。它应该类似于as? [String: Any]
猜你喜欢
  • 2015-02-04
  • 2020-11-23
  • 2020-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-01-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多