ios - 如何使用 Swift 从 Firebase 中的当前用户检索数据?

标签 ios swift firebase firebase-realtime-database firebase-authentication

我正在尝试使用 swift 代码从 firebase 获取当前用户的名字。下面是我的 JSON 文件和快速代码。但这不是检索名字。请建议

JSON structure杰森:

快速代码:

  if FIRAuth.auth()?.currentUser != nil{

        let queryRef = FIRDatabase.database().reference().child("expatsappmembers")


        let userID : String = (FIRAuth.auth()?.currentUser?.uid)!
        print("Current user ID is" + userID)

        queryRef.child("uid").child(userID).observeSingleEvent(of: .value, with: {(snapshot) in
            print(snapshot.value)

            let userName = (snapshot.value as! NSDictionary)["firstname"] as! String
            print(userName)


        })

    }

最佳答案

在 Swift 3 中从 Firebase 获取值略有不同

正确的做法是:

ref.child("expatsappmembers").queryOrderedByKey().observeSingleEvent(of: .value, with: { (snapshot) in

  // Get user value
  let value = snapshot.value as? NSDictionary
  let firstName = value?["firstname"] as? String

  // ...
  }) { (error) in
    print(error.localizedDescription)
}

关于ios - 如何使用 Swift 从 Firebase 中的当前用户检索数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44001903/

相关文章:

ios - 使用 raix :push 发送推送通知时没有任何反应

swift - 同时接收scrollViewDidScroll和UIGestureRecognizer触摸事件

ios - 如何检查 childByAutoID() 是否已存在于 Firebase 实时数据库中?

javascript - Google Firestore - 如何在一次往返中通过多个 ID 获取多个文档?

java - 我们如何在 Android/java 中将新元素推送到 firestore Map 类型数组中?

ios - AudioQueueReset是释放音频缓冲区还是仅返回它们?

ios - 如何在导航栏中添加自定义 View ?

iphone - 双击主页按钮究竟有什么作用?

swift - 快速使用多个 socket

swift - CADisplayLink 捕捉动画有延迟