【发布时间】:2017-08-24 14:44:30
【问题描述】:
我正在使用 swift 3 和 firebase 构建一个应用程序,我想从数据库中检索一个金额进行减法,然后再次更新 firebase 上的金额。我的 firebase 数据结构具有如下所示的功能。
功能:
func updateBal(cred: String){
//creating artist with the new given values
if ( cred != "" ) {
let credit = ["credits": cred]
let newCredit = credit as! Int
let bal = newCredit - 1
//let newBalanceString: [AnyHashable: Any] = [:]
//let newBalanceString: [AnyHashable: Any] = [AnyHashable(bal): "/(bal)"]
let newBalanceString = String(format:"%.2f", bal)
ref.child("users").child(self.user.uid).child("creditdetails").childByAutoId().updateChildValues(newBalanceString)
//displaying message
//LabelMessage.text = "Balance Updated!"
}
}
数据结构
当我取消注释 [AnyHashable: Any] 行时,应用程序在此行崩溃:
let newCredit = credit as! Int
但是当我用[AnyHashable: Any] 注释掉该行时,我在该行得到一个错误:
ref.child("users").child(self.user.uid).child("creditdetails").childByAutoId().updateChildValues(newBalanceString)
错误提示:
无法将 String 类型的值转换为预期的参数类型 [AnyHashable: Any]
您能否指出我如何解决这个问题的正确方向?
【问题讨论】:
标签: ios swift firebase firebase-realtime-database