我目前正在嘗試執行查詢以獲取顯示關注者/關注列表的信息。我目前得到一個錯誤這樣:Could not cast value of type 'FIRDataSnapshot' (0x108967580) to 'NSArray' (0x10b3e3e28).
我是新來的火力和仍在學習的查詢,我的查詢如下:問題與查詢Firebase - Swift
if isFollowers == true {
self.isFollowers = true
ref = ref.child("followers")
let query = ref.queryOrdered(byChild: "userFollow")
query.queryLimited(toLast: 5).observeSingleEvent(of: .value, with: { (snapshot : FIRDataSnapshot) in
if snapshot.childrenCount > 0 {
for s in snapshot.children.allObjects.first as! [FIRDataSnapshot] {
print("are we even in here?")
let item = s.value as! Dictionary<String,AnyObject?>
let user = FollowInfoForUser(dictionary: item as Dictionary<String,AnyObject>)
self.userFollowIndex.insert(user, at: 0)
print(self.userFollowIndex)
//print(self.userFollowIndex.count)
self.collectionView.reloadData()
}
} else {
print("sorry no followers for you to see")
}
})
}
錯誤的路線是這樣的:
for s in snapshot.children.allObjects.first as! [FIRDataSnapshot] {
我樹也如下:
-Users
---UserUID
-----Followers
--------FollowAutoChild
----------------userFollow
----------------userFollowKey
我試圖存儲FollowAutoChild信息
我的整個查詢功能代碼如下:
func setValues(isFollowers : Bool, isFollowing : Bool, isViewingSelf : Bool, isViewingOther : Bool, key : String) {
var ref = FIRDatabase.database().reference().child("users")
if isViewingSelf {
print("we are viewing us")
ref = ref.child(FIRAuth.auth()!.currentUser!.uid)
} else
if isViewingOther {
print("we are viewing them")
ref = ref.child(key)
}
if isFollowers == true {
self.isFollowers = true
ref = ref.child("followers")
let query = ref.queryOrdered(byChild: "userFollow")
query.queryLimited(toLast: 5).observeSingleEvent(of: .value, with: { (snapshot : FIRDataSnapshot) in
if snapshot.childrenCount > 0 {
for s in snapshot.children.allObjects.first as! [FIRDataSnapshot] {
print("are we even in here?")
let item = s.value as! Dictionary<String,AnyObject?>
let user = FollowInfoForUser(dictionary: item as Dictionary<String,AnyObject>)
self.userFollowIndex.insert(user, at: 0)
print(self.userFollowIndex)
//print(self.userFollowIndex.count)
self.collectionView.reloadData()
}
} else {
print("sorry no followers for you to see")
}
})
}
}
的誤差出現,因爲你是鑄造** **第一'snapshot.children'的元素,它只是一個元素(在這種情況下是一個'FIRDataSnapshot'),作爲'FIRDataSnapshot'的數組。既可以將所有對象作爲「FIRDatasnapShot」的數組進行投射,也可以將第一個對象作爲FIRDataSnapshot進行投射,但不能同時投射。 – Eric
非常感謝!我不知道! – AndrewS
很高興能有幫助:) – Eric