Swift 从子i collectionView检索所有值
我正在尝试从一个子项检索所有值到collectionView中 我一直在犯这个错误 无法在此行中强制转换类型为“\uu NSDictionaryM”的值Swift 从子i collectionView检索所有值,swift,firebase,uicollectionview,indexpath,Swift,Firebase,Uicollectionview,Indexpath,我正在尝试从一个子项检索所有值到collectionView中 我一直在犯这个错误 无法在此行中强制转换类型为“\uu NSDictionaryM”的值 let follower = snapshotValue!["Followers"] as! String 这是我的密码 followRef.child("users").child((FIRAuth.auth()?.currentUser?.displayName)!).observe(FIRDataEventType.value, wit
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()
})
followRef.child(“用户”).child((FIRAuth.auth()?.currentUser?.displayName)!).observe(FIRDataEventType.value,带有:{
快照
让snapshotValue=snapshot.value作为字典
让follower=snapshotValue![“Followers”]作为!字符串
self.postsFollow.insert(postFollowStruct(跟随者:跟随者),位于: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
}
}
}
@CarstenHøvsgaard,它是一个类对象。Follower()是实例化它的方法。然后调用setValuesForKeys(restDict)将这些值应用于其属性。如果restDict具有值为“Carsten”的键“Name”,那么followerbj将具有这些值。类中必须有属性“Name”,否则它将崩溃。然后将该对象提供给collectionView。如果那是你想做的。