我正在尝试将一个子项的所有值检索到 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/