我如何从Firebase数据库获得的[String:Any]词典中的status值不为nil?

时间:2018-06-27 08:29:57

标签: swift firebase firebase-realtime-database nsdictionary

 self.ref.child("Users").queryOrdered(byChild: "phoneNumber").queryEqual(toValue: self.userMobile).observeSingleEvent(of: .value, with: { snapshot in

 if snapshot.value is NSNull{

    print("Not exists in database")
    self.performSegue(withIdentifier: "segue1", sender: self)
 }

 else{
     // Get user value
     let value = snapshot.value as! [String : Any]

     print(value)
     let status = value["status"] as? Int

     print ("User status is : \(status)")
     if status == 0 {

         self.performSegue(withIdentifier: "segue1", sender: self)
     }                 
     else if status == 1 {                       
         self.performSegue(withIdentifier: "segue2", sender: self)
     }

error.stack full error message

1 个答案:

答案 0 :(得分:1)

那是因为您得到的不仅仅是一个用户,而是一个用户列表(如果您只有一个用户,则只有一个用户列表)。

做这样的循环:

for user in value.children.allObjects as! [DataSnapshot]
{
    let status = user.childSnapshot(forPath: "status").value as? Int
    print ("User status is : \(status)")
}