arrays - 使用数组中的所有值执行任务(Swift、Firebase)
问题描述
我的情况是我正在创建一个社交媒体应用程序,并且我有一系列用户正在关注的人。它看起来像这样:
let followingArray:[String] = ["user1Uid", "user2Uid", "user3Uid", "user4Uid"]
因此,我想使用数组中的每个 userUid 执行一个函数来获取他们的帖子并将其显示到 collectionView 上。
为了提供更多上下文,函数看起来像这样(“userUid”是我需要与所有使用数组的 userUid 重复的元素):
databaseRef.child("posts").child(userUid).observeSingleEvent(of: .value) { (snapshot) in
// all of the code to get the posts information into another array to display on the collection view.
}
非常感谢!
解决方案
要对数组中的每个元素执行某些操作,您始终可以使用forEach(_:)。尝试:
let followingArray:[String] = ["user1Uid", "user2Uid", "user3Uid", "user4Uid"]
for userID in followingArray {
databaseRef.child("posts").child(userID).observeSingleEvent(of: .value) { (snapshot) in
// all of the code to get the posts information into another array to display on the collection view.
}
}
推荐阅读
- html - 如何在 Outlook 桌面(Outlook 2007)中垂直对齐同一 TD 中的图像(标签内)和文本
- python - 从 Python 读取 Mac .numbers 文件
- javascript - 我正在尝试将表格中的总数加在一起,但不知道该怎么做
- javascript - React 没有安装
- docker - 我无法让我的 docker 设置工作,我做错了什么
- apache-flink - 来自 KAFKA KRB5 Kerberos 问题的 FLINK 流式传输
- ios - 本地化运行时间不变
- reporting-services - 对 Visual Studio 和 Microsoft Report Builder 中的 SSRS 之间的差异感到困惑
- vb.net - Dictionary 对象的设计时编辑(WinForms)
- ios - 如何替换 SwiftUI 中的当前视图?