2016-10-03 34 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