【发布时间】:2019-05-03 17:29:08
【问题描述】:
我正在使用 pod CodableFirebase 解码 Firebase 数据,并尝试将该数据放入数组中。我遇到的问题是将每个数据实例放入一个单独的数组中,当我转到 IndexPath 以在 CollectionView 中使用它时导致我出现问题。
代码:
struct WatchList: Codable {
let filmid: Int?
}
var watchList = [WatchList]()
ref.child("users").child(uid!).child("watchlist").observe(DataEventType.childAdded, with: { (info) in
guard let value = info.value else { return }
do {
let list = try! FirebaseDecoder().decode(WatchList.self, from: value)
self.watchList = [list]
print(self.watchList)
self.watchlistCollection.reloadData()
}
}, withCancel: nil)
数组是如何打印到控制台的:
[Film_Bee.ProfileView.WatchList(filmid: Optional(332562))]
[Film_Bee.ProfileView.WatchList(filmid: Optional(369972))]
[Film_Bee.ProfileView.WatchList(filmid: Optional(335983))]
当我在 CollectionView 中使用数组时,它只计算最后一个数组的索引路径。
如何将数据放入单个数组中?
【问题讨论】:
-
您可能希望将项目添加到数组中:
self.watchList.append(list) -
好的,所以这适用于将项目放置在数组中。问题在于,它会一次又一次地将一个项目放入数组中,然后创建一个数组。这意味着如果我有 100 个要放置的项目,它将在完成之前创建 100 个数组。这可能会导致问题,是否有原因一次只添加一项?
-
打印如下:[item 1] [item 1, item 2] [item 1, item 2, item 3]
标签: arrays swift firebase firebase-realtime-database