swift - 从子 Collection View 中检索所有值

标签 swift firebase uicollectionview indexpath

我正在尝试将一个子项的所有值检索到 CollectionView 中

我不断收到此错误

无法在此行中转换类型“__NSDictionaryM”的值

let follower = snapshotValue!["Followers"] as! String

这是我的代码

followRef.child("users").child((FIRAuth.auth()?.currentUser?.displayName)!).observe(FIRDataEventType.value, with: {
            snapshot in
            let snapshotValue = snapshot.value as? Dictionary<String,AnyObject>
            let follower = snapshotValue!["Followers"] as! String
            self.postsFollow.insert(postFollowStruct(follower: follower) , at: 0)
            self.followersView.reloadData()
            })

最佳答案

据我了解,您的 snapshot.value 中有许多 json 对象?然后你想将它们每个都转换为一个对象并将它们放入一个数组中?

if let dict = snapshot.value as? [String: AnyObject]{
                    let followers = dict
                    for follower in followers {
                        if let restDict = follower.value as? [String:AnyObject] {
                            let followerObj = Follower()
                            followerObj.setValuesForKeys(restDict)
                            //add to array
                        }
                    }
                }

关于swift - 从子 Collection View 中检索所有值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43160778/

相关文章:

javascript - 向 firebase 生成的 key 内的子项添加监听器?

android - 云 java.io.FileNotFoundException : WEB-INF/logging. 属性(没有这样的文件或目录)

ios - 在 iOS 7 上创建类似 Evernote 的弹性 UICollectionView

ios - 屏幕旋转后如何调用sizeForItemAtIndexPath方法

ios - 使用关联类型创建元组

iphone - NSUserDefaults 到 Apple Watch

MacOS 上的 Swift - 如何将 PHP post 函数的响应保存到全局变量

firebase - Flutter:如何在firebase中删除特定的数组数据

ios - 仅水平滚动图像的登录页面

ios - 如何将 subview 添加到自定义 UICollectionViewCell