Firebaseから取り込んだオブジェクトを保存する際に問題が発生しました。私は 'cardAmount'の価値をつかみ、そこに追加して保存したいと思います。しかし、数字を文字列に変換するのは難しくありません。私は間違ったデータ構造を変更することに悩まされていたかもしれないと思いますが、私はちょっとの間これに固執しています - 誰かに私が必要なときにFirebaseの単一のチャイルドスナップショットを編集する方法に関するガイダンスをくれますか?Swiftを使用してFirebaseで既存のオブジェクトを更新する3
let sevenDayOption = UIAlertAction(title: "Seven Day: $31", style: .default, handler: {
(alert: UIAlertAction!) -> Void in
let query = ref.queryOrdered(byChild: (FIRAuth.auth()!.currentUser?.uid)!)
query.observeSingleEvent(of: .value, with: { (snapshot) in
let cardAmount = snapshot.childSnapshot(forPath: "cardAmount").value as! Float
let newAmount = cardAmount + 31
let newAmountString = String(format:"%.2f", newAmount)
snapshot.setValue(newAmountString, forKeyPath: snapshot.childSnapshot(forPath: "cardAmount").value as! String)
print(snapshot.childSnapshot(forPath: "cardAmount").value as! Float) /*<---- Prints Optional(30.5)*/
})
})
私はそれがおそらく単純なものだと確信していますが、それは現時点では私を逃すものです。 FirebaseとSwiftには比較的新しいので、時にはそれと苦労しています。
ありがとうございます!この渡って来ると、将来のトラブルを持っている人のために
があります** runTransactionBlock **です。または** updateChildValues **メソッド – Wolverine
updateChildValuesを使用しても問題ありません。答えを提出する。どうもありがとうございます! –