代码之家  ›  专栏  ›  技术社区  ›  fatimah shams

Firebase实时数据库数据获取

  •  0
  • fatimah shams  · 技术社区  · 6 年前

    我正在尝试获取以下值作为变量

    enter image description here

    但我的代码给我零

    ref.child("users").child(userID!).observeSingleEvent(of: .value, with: { (snapshot) in
        // Get user value
        let value = snapshot.value as? NSDictionary
        let userInfo = value?["UserInfo"] as? NSDictionary
        let data = userInfo?["lat"] as? String ?? ""
        print(data)
    
    
        // ...
    }) { (error) in
        print(error.localizedDescription)
    }
    

    1 回复  |  直到 6 年前
        1
  •  1
  •   DionizB    6 年前

    您只需在此处做一个小改动:

    let data = userInfo?["lat"] as? String ?? ""
    

    Latitude不是字符串,而是Int,因此转换为Int:

    let data = userInfo?["lat"] as? Int ?? 0