Firebase检查空值(Swift) [英] Firebase checking for null value (Swift)

查看:123
本文介绍了Firebase检查空值(Swift)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在运行下面的代码,看看是否有用户打开应用程序已经登录,然后检查他们是否已经建立了一个配置文件。我无法检查从配置文件检查返回的空值的值。

$ p $覆盖func viewDidLoad(){
super.viewDidLoad()

//检查用户是否已经登录
//通过检查Firebase来查看authData是否不为
如果ref.authData!=无{

//如果用户已经登录,请获取他们的uid。
//然后检查Firebase以查看uid是否已经创建了配置文件
let uid = ref.authData.uid
ref.queryOrderedByChild(uid)。q​​ueryEqualToValue(uid)。 observeSingleEventOfType(.Value,withBlock:{snapshot in
let profile = snapshot.value
print(profile)
})

 

< null>

如何检查这个值?

< pre $ 如果profile == nil

不起作用

p>

如果我这样做

$ $ p $ $ code>让profile = snapshot.value as?字符串

首先,它总是返回nil,即使有snapshot.value

解决方案

利用exists()方法来确定快照是否包含值。使用你的例子:

  let uid = ref.authData.uid 
ref.queryOrderedByChild(uid)。 queryEqualToValue(uid)
.observeSingleEventOfType(.Value,withBlock:{snapshot in
$ b $ guard snapshot.exists()else {
print(User does not exist)
返回
}

print(User \(snapshot.value)exists)
})
pre>



这是另一个方便的例子,Swift4

  let path =userInfo /+ id +/ followers /+ rowId 

let ref = Database.database()。reference(withPath:path)

ref.observe(.value) {(snapshot)in

let below:Bool = snapshot.exists()

icon = yesWeAreFollowing? tick:cross
}


I'm running the code below to see if a user that opens the app is already logged in, and then to check if they've set up a profile. I'm having trouble checking for the null value value returned from the profile check

override func viewDidLoad() {
    super.viewDidLoad()

    //Check to see if user is already logged in
    //by checking Firebase to see if authData is not nil
    if ref.authData != nil {

        //If user is already logged in, get their uid.
        //Then check Firebase to see if the uid already has a profile set up
        let uid = ref.authData.uid
        ref.queryOrderedByChild("uid").queryEqualToValue(uid).observeSingleEventOfType(.Value, withBlock: { snapshot in
                let profile = snapshot.value
                print(profile)
        })

In the last line when I print(profile), I either get the profile information, or

 <null>

how do i check for this value?

 if profile == nil 

does not work

If I do

let profile = snapshot.value as? String

first, then it always returns nil even if there is a snapshot.value

解决方案

Take advantage of the exists() method to determine if the snapshot contains a value. To use your example:

let uid = ref.authData.uid
ref.queryOrderedByChild("uid").queryEqualToValue(uid)
         .observeSingleEventOfType(.Value, withBlock: { snapshot in

    guard snapshot.exists() else{
        print("User doesn't exist")
        return
    }

    print("User \(snapshot.value) exists")
})

.

Here's another handy example, Swift4

    let path = "userInfo/" + id + "/followers/" + rowId

    let ref = Database.database().reference(withPath: path)

    ref.observe(.value) { (snapshot) in

            let following: Bool = snapshot.exists()

            icon = yesWeAreFollowing ? "tick" : "cross"
        }

这篇关于Firebase检查空值(Swift)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆