2016-10-03 30 views
0

我想从我的JSON树下,火力地堡一些价值观但是我得到错误:转换火力地堡值(INT)为String导致错误

Could not cast value of type '__NSCFNumber' (0x1a7c497f0) to 'NSString' (0x1a7c55398).

我设置的值的方式是这样的:

let values = ["username": username, "email": email, "reputation": reputation] as [String : Any] 

我得到这样的值:

self.databaseRef.child("users").child(self.loggedInUser!.uid).observeSingleEvent(of: .value) { (snapshot:FIRDataSnapshot) in 

      //store the logged in users details into the variable 

      print(self.loggedInUser) 
      let snapshot = snapshot.value as! [String: AnyObject] 
      self.usernameLabel.text = snapshot["username"] as! String 
      self.reputationLabel.text = snapshot["reputation"] as! String//This is giving me error 
}) 

我做错了吗?这是什么造成的?在Swift 2中它工作,但不在Swift 3中。

回答

1

您从firebase中检索的数据可能是double类型的。尝试使用这样的:

self.reputationLabel.text = "\(snapshot["reputation"])" 

编辑:

,如果这是给你的NIL,尽量使用条件展开:

if let reputation = snapshot["reputation"] { 
    self.reputationLabel.text = "\(reputation)" 
} else { 
    print("reputation is nil") 
} 

如果你的数据是零,然后在火力检查你的数据名称,和你的对象在db