我无法从Firebase获取数据。我尝试从Firebase获取数据。 除了这两行,一切都很好。
func calculateDailyAmountOfCalories() {
let uid = Auth.auth().currentUser?.uid
let ref = Database.database().reference()
ref.child("users").child(uid!).observe(.value) { (snapshot) in
self.userWeight = snapshot.childSnapshot(forPath: "weight").value as! Float
self.userHeight = snapshot.childSnapshot(forPath: "height").value as! Float
self.userAge = snapshot.childSnapshot(forPath: "age").value as! Float
**var newUserGoal = snapshot.childSnapshot(forPath: "userGoal").value as? Float
var newUserLevelActivity = snapshot.childSnapshot(forPath: "activityLevel").value as? Float**
print(newUserGoal)
print(newUserLevelActivity)
print(self.userWeight)
print(self.userHeight)
}
}
当我尝试获取(var newUserGoal和newUserLevelActivity)data without as?/! Float
时,我可以在终端Optional(0.2)中看到一个answear。当我尝试使用as?
时,结果为零。
为什么其他类似的快照可以工作,但为什么不能呢?