OGeek|极客世界-中国程序员成长平台

标题: ios - 将 UICollectionView 填充到 Firebase [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-11 18:31
标题: ios - 将 UICollectionView 填充到 Firebase

我正在尝试使用 Firebase 数据库中的数据填充 Collection View 。我正在我的 viewdidload 函数中进行以下调用:

ref = FIRDatabase.database().reference(withPath: "rofiles")

handle = ref.queryOrdered(byChild: title!).observe(.value, with: { snapshot in

    var items: [Profiles] = []

    if snapshot.value is NSNull {

       print("NIL!!")

    } else {
       for item in snapshot.children {

           let profile = Profiles(snapshot: item as! FIRDataSnapshot)

               items.append(profile)
           }

            self.profiles = items
            self.collectionView?.reloadData()
        }
    }) { (error) in
        print(error.localizedDescription)
    }

我收到以下错误:

fatal error: unexpectedly found nil while unwrapping an Optional value

编译器会突出显示以下代码:

handle = ref.queryOrdered(byChild: title!).observe(.value, with: { snapshot in

我不明白问题是什么,也不知道如何解决?!



Best Answer-推荐答案


听起来您将 UIViewControllertitle 成员与您使用查询的 Firebase 子项混淆了。如果您的数据结构中有一个名为 title 的子节点,并且想要基于它进行查询,只需将查询语句替换为:

ref.queryOrdered(byChild: "title").observe(.value, with: { snapshot in
    // your code
})

如果您使用的变量 (title) 为 nil,则您得到错误的唯一方法是。

关于ios - 将 UICollectionView 填充到 Firebase,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41516814/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4