我需要從Firebase獲取一些信息才能在UIViewCell
上找到它。但問題是,在他獲得所有價值之前,函數會返回。我知道那是因爲它是異步的,我試圖做一個完成處理這樣的:如何使用Firebase Swift製作CompletionHandler 3
static func hardProcessingWithString(input: String, completion: @escaping (_ result: String) -> Void) {
Database.database().reference().child("Player_1").observe(.value) {
(firDataSnapshot) in
completion((firDataSnapshot.value as? String)!)
}
}
這是我嘗試獲得價值:
var myVar: String!
hardProcessingWithString(input: "commands"){
(result: String) in
myVar = result
}
當我打電話的功能我得到這個錯誤:
無法將'__NSDictionaryM'(0x102e292b0)類型的值轉換爲'NSString'(0x102434c60)。
這裏是我的火力地堡數據庫:
或者,如果你知道如何使火力地堡許諾讓我知道!
告訴我們孩子的數據庫結構法 「PLAYER_1」。錯誤不在完成處理程序中,但在類型中。像db –
的屏幕截圖是firDataSnapshot.value實際上是一個字符串?你能調試這個值嗎? – petul
@petul是的,它是一個字符串! – Max0u