這是我試圖從Firebase檢索代碼並對其執行操作的第二個版本。這是我如何做它的第二種方式:無法從Firebase正確檢索數據
channelRef?.observe(.childChanged, with: { (snapshot) -> Void in
let update = snapshot.value as! Dictionary<String, AnyObject>
var readyToGoValue: Bool?
var userID: String?
var amountOfPlayers: Int?
var changedCreator: String?
if let updatedReadyToGo = update["readyToGo"] as! Bool!{
if updatedReadyToGo == true
{
readyToGoValue = true
}
else
{
readyToGoValue = false
}
}
if let updateduserID = update["userID"] as! String!{
userID = updateduserID
}
if let updatedAmountOfPlayers = update["currentPlayers"] as! Int!{
amountOfPlayers = updatedAmountOfPlayers
}
if let updateduserID = update["userID"] as! String!{
userID = updateduserID
}
if let updatedCreator = update["creator"] as! String!{
changedCreator = updatedCreator
}
let currentUser = FIRAuth.auth()?.currentUser?.uid
if changedCreator != nil
{
print("changed creator")
self.creator = changedCreator!
}
這開車撞錯誤代碼:
無法投型「__NSCFString」(0x10a77f4a0)爲「NSDictionary中」(0x10a780288)的價值。在「更新」行。這是我第一次嘗試:
channelRef?.observe(.childChanged, with: { (snapshot) -> Void in
let value = snapshot.value as? NSDictionary
let readyToGoValue = value?["readyToGo"] as? Bool ?? false
let userID = value?["userID"] as? String ?? ""
var amountOfPlayers = value?["currentPlayers"] as? Int ?? 0
let changedCreator = value?["creator"] as? String ?? ""
print(snapshot)
let currentUser = FIRAuth.auth()?.currentUser?.uid
print(changedCreator)
print(amountOfPlayers)
if changedCreator != ""
{
print("changed creator")
self.creator = changedCreator
}
這不起作用。
快照(創造者)喜
但是打印(「改變的創造者」)從未被執行:當改變在火力地堡的創造者(只是一個字符串),我添加打印時(快照)得到這個的圖片。爲什麼是這樣?
編輯:這是我如何得到channelRef ?:
super.prepare(for: segue, sender: sender)
if let channel = sender as? Channel {
let chatVc = segue.destination as! channelMultiplayerViewController
chatVc.channel = channel
chatVc.channelRef = channelRef.child(channel.id)
chatVc.usersKey = userKey
}
打印的詳細數據:
print("path channel ref: " + "\(self.channelRef)")
print("snapshot: " + "\(snapshot)")
print("value: " + "\(value)")
-path通道編號:可選(https://X.com/channels/-KeGKaJavH6uPYaSa7k4)
-snapshot:快照(創作者)新造物主
-value:無
更新:
數據結構:
這將爲現在的工作,但是是不是有更好的辦法?:
if snapshot.key == "creator"
{
changedCreator = snapshot.value as! String
}
另一個問題完全一樣,但上面與第一個問題的解決方案,這個問題就不會得到解決。當我嘗試獲取第一個子節點,所以第一個用戶,並試圖獲得他們的用戶ID,沒有任何工作。我使用這個代碼:
let firstChild = UInt(1)
self.channelRef?.queryLimited(toFirst: firstChild).observeSingleEvent(of: .value, with: { (snapshot) in
let value = snapshot.value as? NSDictionary
print(snapshot)
print(value)
let newCreator = value?.value(forKey: "userID") as? String
if newCreator != nil{
print("Got the userID")
}
if snapshot.key == "userID"
{
print("Got the userID")
}
})
Snap (-KeJWMiXaL-FGp0J7b3u) {
"-KeJWO0V9kxgGnrACAtP" = {
PictureVersion = 2;
readyToGo = 0;
userID = SZlQ76RLCJQpFa0CDhrgFJoYzrs2;
username = pietje;
};
}
Optional({
"-KeJWO0V9kxgGnrACAtP" = {
PictureVersion = 2;
readyToGo = 0;
userID = SZlQ76RLCJQpFa0CDhrgFJoYzrs2;
username = pietje;
};
})
而這打印出來,所以沒有用戶ID給出。爲什麼是這樣?用戶標識就在那裏!我閱讀文檔,但它應該工作...
更換
chatVc.channelRef = channelRef.child(channel.id)
您可以包括行你設置'channelRef?'變量? –可能你給它的路徑太深入到數據庫結構的一個層次,比如''some_collection/an_object/readyToGo「'而不是''some_collection/an_object」',這可以解釋爲什麼'snapshot'被返回爲一個字符串而不是字典 –
我編輯了這個問題。將數據更改爲字典時出現錯誤我認爲 – Petravd1994