Firebase {
clickedCounter: 0< br />}
This is my code:
@IBAction func plus(sender: UIButton) {
FIRDatabase.database() .reference().child("clickedCounter").observeEventType(.Value) {(snap: FIRDataSnapshot) in
let value = FIRDatabase.database().reference().child("clickedCounter") as? NSNumber
let increment = ((value?.intValue)! + ((1 as? NSNumber)?.intValue)!) as? NSNumber
FIRDatabase.database().reference().child("clickedCounter ").setValue()
self.clickLabel.text = snap.value?.description as String!
}
}
I also tried:
@IBAction func plus(sender: UIButton) {
FIRDatabase.database().reference().child("clickedCounter").observeEventType(.Value) {(snap : FIRDataSnapshot) in
FIRDatabase.database().reference().child("clickedCounter").setValue(( snap.value?.description)! + 1)
self.clickedLabel.text = snap.value?.description as! String!
}
}
My The first warning is:
Conversion from Int32 to NSNumber always fails
The conditional actor from NSNumber to NSNumber always succeeds
Conversion from FIRDatabaseReference to NSNumber always fails
After I run it, I get an error about let increment…: Thread 1: EXC_BAD_INSTRUCTION
The second error for me is:
p>
Binary operators cannot be applied to operands of type String and Int
>When I think I define it as Int in Firebase, I am confused about my snap.value? .description is a string.
>Why do I get a warning when things always succeed?
>Is there a better way?
Thank you!
counterRef.observeSingleEventOfType(.Value, withBlock: {snapshot in
let valString = snapshot.value
var value = valString.intValue
value = value + 1
counterRef.setValue("\(value)")
})
If multiple users update frequently, you can also use the transaction block.
I have trouble adding a piece of data to Firebase.
Firebase {
clickedCounter: 0
}
This is my code:
@IBAction func plus(sender: UIButton) {
FIRDatabase.database().reference( ).child("clickedCounter").observeEventType(.Value) {(snap: FIRDataSnapshot) in
let value = FIRDatabase.database().reference().child("clickedCounter") as? NSNumber
let increment = ((value?.intValue)! + ((1 as? NSNumber)?.intValue)!) as? NSNumber
FIRDatabase.database().reference().child("clickedCounter"). setValue()
self.clickLabel.text = snap.value?.description as String!
}
}
I also tried:
@IBAction func plus(sender: UIButton) {
FIRDatabase.database().reference().child("clickedCounter").observeEventType(.Value) {(snap: FIRDataSnapshot) in
FIRDatabase. database().reference().child("clickedCounter").setValue((snap.value?.description)! + 1)
self.clickedLabel.text = snap.value?.description as! String!< br /> }
}
My first warning is:
The conversion from Int32 to NSNumber always fails
From NSNumber to NSNumber The conditional actor always succeeds
The conversion from FIRDatabaseReference to NSNumber always fails
After I run it, I get an error about let increment…: Thread 1: EXC_BAD_INSTRUCTION
The second error for me is:
Binary operators cannot be applied to operands of type String and Int
>When I think I’m in Firebase I am confused about my snap.value when I define it as an Int. .description is a string.
>Why do I get a warning when things always succeed?
>Is there a better way?
Thank you!
Why not (v2.x Firebase code, but you get it)
counterRef .observeSingleEventOfType(.Value, withBlock: {snapshot in
let valString = snapshot.value
var value = valString.intValue
value = value + 1
counterRef.setValue("\ (value)")
})
If multiple users update frequently, you can also use transaction blocks.