【发布时间】:2016-03-09 04:00:44
【问题描述】:
我正在运行下面的代码来查看打开应用程序的用户是否已经登录,然后检查他们是否设置了个人资料。我在检查配置文件检查返回的空值时遇到问题
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)
})
在我打印(配置文件)时的最后一行,我要么获取配置文件信息,要么
<null>
我如何检查这个值?
if profile == nil
没用
如果我这样做
let profile = snapshot.value as? String
首先,即使有snapshot.value,它也总是返回nil
【问题讨论】:
-
尝试 if (snapshot.value != nil){ } 或 if (snapshot.value as!NSObject != nil){ }
-
感谢 shrikant,但首先遇到了同样的问题。第二个建议给了我“'NSObject 类型的值永远不能为零”错误
-
快照的数据类型是什么?
-
如果 profile == NSNull() 你试过了吗?
-
在快速检查 nil 值时,首先您必须使用 '?' 将其设为可选。然后你可以这样做 if let profile = snapshot.value { print(profile) }