title: turn a firebase database value into an Int
body: <p>In my app people can make bets on which team they think will win. When they go to make a bet a couple of things should be uploaded to the database. First the username of the person making the bet. Which bet this is( is it bet one bet two bet three) and it should show what team the bet is for.</p>
  
  <p>The problem I have turning the bet into an Int so that I can increment it every time someone makes a bet so that they have a unique bet. </p>
  
  <p>below I have a couple of things that I will explain because my code is jumbled up at the moment because of testing purposes.</p>
  
  <p>in the viewDidLoad method I have the means to gather the user name and how many bets that someone has made. in the database this defaults as a string "0".</p>
  
  <p>now bet self.betNumber should have the value "0"</p>
  
  <p>then in the submitData function I safely take the betNumber and turn it into an Int then assign that value to the globally declared variable bets </p>
  
  <p>then I increment bets by 1
  after that I try to cast self.bets into a String and but it into the database as a child</p>
  
  <p>and this is where I get an error</p>
  
  <pre><code>import UIKit
  import Foundation
  import Firebase
  
  
  class addBets: UIViewController {
      let betRef = FIRDatabase.database().reference(fromURL: "not showing these sorry")
      let userRef = FIRDatabase.database().reference(fromURL: "")
      let ref = FIRDatabase.database().reference(fromURL: "")
      let currentUser = FIRAuth.auth()?.currentUser
  
  
      @IBOutlet weak var teamOneLabelAdd: UILabel!
      @IBOutlet weak var teamTwoLabelAdd: UILabel!
  
      @IBOutlet weak var teamOneBet: UITextField!
  
      @IBOutlet weak var teamTwoBet: UITextField!
  
  
  
      var userName: String!
      var teamOne: String!
      var teamTwo = String()
  
      var betNumber: String!
      var bets: Int = 0
  
  
  
  
      var teamOneBetAnotherOne: String!
      var teamOneBetAnotherTwo: String!
      override func viewDidLoad() {
          super.viewDidLoad()
          teamOneLabelAdd.text = teamOne
          teamTwoLabelAdd.text = teamTwo
  
  
          //Looks for single or multiple taps.
          let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: "dismissKeyboard")
  
          //Uncomment the line below if you want the tap not not interfere and cancel other interactions.
          tap.cancelsTouchesInView = false
  
          view.addGestureRecognizer(tap)
  
          //self.teamOneBet.delegate = self
          //self.teamTwoBet.delegate = self
  
          let userID = FIRAuth.auth()?.currentUser?.uid
          ref.child("User").child(userID!).observeSingleEvent(of: .value, with: { (snapshot) in
              // Get user value
              let value = snapshot.value as? NSDictionary
              let username = value?["username"] as? String ?? ""
              self.userName = username
              let betnumber = value?["bets"] as? String ?? ""
              self.betNumber = betnumber
  
  
  
  
              // ...
          }) { (error) in
              print(error.localizedDescription)
          }
  
  
      }
  
      func textFieldShouldReturn(userText: UITextField!) -&gt; Bool {
          teamOneBet.resignFirstResponder()
          teamTwoBet.resignFirstResponder()
          return true
      }
  
  
      func dismissKeyboard() {
          //Causes the view (or one of its embedded text fields) to resign the first responder status.
          view.endEditing(true)
      }
  
      @IBAction func submitData(_ sender: Any) {
          // get the userName to tell who is doing the bet and set it as a key
  
          sleep(4)
  
          if let usernamee = self.userName {
              if let anotherBet = self.betNumber as? Int {
                  self.bets = anotherBet
              }
              self.bets += 1
  
              let usableBets = self.bets as? String ?? ""
  
              let useRef = self.ref.child("Bets").child(usernamee).child(self.bets as? String ?? "")
  
              var values = ["Username": usernamee,"Bet": "0", "ForTeam": ""]
              if let betOne = self.teamOneBet.text{
  
                  if betOne == "" {
  
                  } else {
                      values["Bet"] = betOne
                      values["ForTeam"] = teamOneLabelAdd.text
                  }
              }
              if let betTwo = self.teamTwoBet.text{
  
                  if betTwo == "" {
  
                  } else {
                      values["Bet"] = betTwo
                      values["ForTeam"] = teamTwoLabelAdd.text
                  }
              }
  
  
          //self.betRef.child("Bets").setValue(self.userName)
          if let worked = currentUser?.getTokenWithCompletion() {
  
              useRef.setValue(values)
              /*
              useRef.updateChildValues(values, withCompletionBlock: { (error, ref) in
                  if (error != nil) {
                      print(error!)
                      return
  
                  }
  
              })
              */
  
  
  
          }
  
  
           self.navigationController?.popViewController(animated: true)
  
          }
  
  
  
  
  
  
  
      }
  
      func getUserName() {
          let userID = FIRAuth.auth()?.currentUser?.uid
          ref.child("User").child(userID!).observeSingleEvent(of: .value, with: { (snapshot) in
              // Get user value
              let value = snapshot.value as? NSDictionary
              let username = value?["username"] as? String ?? ""
              self.userName = username
  
              // ...
          }) { (error) in
              print(error.localizedDescription)
          }
      }
  
  
      override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
          //let indexPath = self.tableView.indexPathForSelectedRow!
          //var DestViewController : addBets = segue.destination as! addBets
          //DestViewController.teamOne = self.teamOne.text!
          //DestViewController.teamOne = self.teamTTwo
  
          if segue.identifier == "gotobetview" {
              if let destination = segue.destination as? BetView {
  
                  //destination.userNames = self.teamOne.text! as? String
  
                  //destination.teamTwo = (self.teamTwo.text! as? String)!
              }
          }
  
      }
  
  }
  </code></pre>
  
  <p>and here is the firebase structure the data I am extracting is coming from the "User" and the data I am writing to is "Bets"</p>
  
  <p><a href="https://i.stack.imgur.com/Eqymi.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Eqymi.png" alt="enter image description here"></a></p>