0

I want to get a value from my Firebase Database and add 10 points to it after the user clicks on a button.

my Firebase Database looks like:

usersaddclose
    profile
     -DiGPeQWLUkR35YjvTYmS9SxWPQo1
       fullname: 
       pathToImage: 
       points: 23 (need to get this value)
       username: 

code below doesn't work. With the error: Could not cast value of type 'NSTaggedPointerString' (0x112b74560) to 'NSNumber' (0x1113dcd40). I'm not sure how else to structure it.

@IBAction func commentBtn(_sender: Any) {

 let bef = Database.database().reference().child("users/profile/\(uid)/points")

 bef.observeSingleEvent(of: .value, with: { (snapshot) in 
            var db = snapshot.value as! Int
            db += 10
            print(db)
 })

}

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • From the error message it looks like you store a string value in `points`, not a number. In that case you'll need to parse the string into a number, as shown here: https://stackoverflow.com/a/30802236/209103 – Frank van Puffelen Oct 13 '19 at 00:20
  • To actually read-the-value-and-write-the-incremented-value you'll want to [use a database transaction](https://firebase.google.com/docs/database/ios/read-and-write#save_data_as_transactions) btw. – Frank van Puffelen Oct 13 '19 at 00:23

0 Answers0