从Firebase数据库检索Integer一无所获

时间:2019-01-13 18:14:47

标签: ios swift firebase

因此,我正在从Firebase数据库中检索数据(整数)并将其打印为标签。但是,在检索数据并将其设置为标签的文本之后,它只是空白(只是空白)。为什么会这样?

玩过的游戏是一个整数

数据库Json文件:

{
  "mix-questions" : [ {
    "1" : "Eu Ro Wang",
    "2" : "Keerti Soundappan",
    "3" : "Galadriel Coury",
    "4" : "Madelyn Koch",
    "answer" : "Madelyn Koch",
    "question" : "Which one is not a national officer?"
  }, {
    "question" : "test"
  } ],
  "users" : {
    "0vFQYnfIbahHdC1RsuNMW45HYNR2" : {
      "email" : "andyjiang55@yahoo.com",
      "username" : "itsandyjiang"
    },
    "Cs7sW01kSEYHYZmBISvjYc1QmDy1" : {
      "email" : "test@gmail.com",
      "games-played" : 0,
      "username" : "test"
    },
    "GypKikOJbfOPV4m43mQTnLMWBYG3" : {
      "email" : "andyjiang@yahoo.com",
      "games-played" : 0,
      "username" : "andyjiang"
    },
    "Khf6jRX7uuZs40x8w7zapziCsr73" : {
      "email" : "andyjiang57@yahoo.com",
      "games-played" : 0,
      "username" : "itsandyjiang3"
    },
    "OAa5bbNc65acxmr6mauyIQ6bqHb2" : {
      "email" : "andyjiang56@yahoo.com",
      "games-played" : 0,
      "username" : "itsandyjiang2"
    },
    "YxxBBxS2WkWtrY8PnLx4jfDqV3o2" : {
      "email" : "andyjiang1@yahoo.com",
      "games-played" : 0,
      "username" : "itsandyjiang5"
    },
    "taikB5Gp1kaAmuPpiHxPoiKmjf03" : {
      "email" : "andyjiang2@yahoo.com",
      "games-played" : 0,
      "username" : "itsandyjiang9"
    }
  }
}

检索数据(“玩游戏”)并将其设置为标签的文本:

 func render() {
    let ref = Database.database().reference(fromURL: "https://testtest.firebaseio.com/")
    let userID = Auth.auth().currentUser?.uid
    ref.child("users").child(userID!).observeSingleEvent(of: .value, with: { (snapshot) in
        // Get user value
        let value = snapshot.value as? NSDictionary
        let username = value?["username"] as? String ?? ""
        let gamesPlayed = value?["games-played"] as? String ?? ""
        self.usernameLabel.text = username.  
        self.gamesPlayedLabel.text = gamesPlayed  //Setting of label from data

        // ...
    }) { (error) in
        print(error.localizedDescription)
    }
}

设置数据的位置:

let ref = Database.database().reference(fromURL: "https://testtest.firebaseio.com/")
                let usersReference = ref.child("users").child(user.uid)
                let values = ["username": self.usernameTextField.text!, "email": self.emailTextField.text!, "games-played": 0] as [String : Any]
                usersReference.updateChildValues(values, withCompletionBlock: { (err, ref) in
                    if err != nil {
                        print(err!)
                        return
                    }

                    //Successfully registered user's data to database

                    self.errorLabel.alpha = 0
                    self.present((self.storyboard?.instantiateViewController(withIdentifier: "TabBarViewController"))!, animated: false, completion: nil)


                })

1 个答案:

答案 0 :(得分:0)

let gamesPlayed = value?["games-played"] as? String ?? "" 可能转换为字符串失败,您应该做的是as? Int,然后转换为字符串