在不可选的情况下获取可选的Firebase数据库值 [英] Getting an optional firebase database value when it shouldn't be optional

查看:36
本文介绍了在不可选的情况下获取可选的Firebase数据库值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在运行以下查询以从快照获取值,但是该值作为可选返回.

I am running the following query to get a value from the snapshot, but the value is returning as optional.

ref.child("PGroups").observeSingleEventOfType(.Value, withBlock: { (snapshot) in
    let groupName = String(rest.childSnapshotForPath("/GroupName").value)
    print(groupName)
})

并得到以下打印声明:"Optional(Name)" 推荐为:"Name"

and am getting the following printed statement: "Optional(Name)" as appoosed to just: "Name"

推荐答案

只需添加一个感叹号,告诉编译器解开可选内容:

Simply add an exclamation point to tell the compiler to unwrap the optional:

print(groupName!)应该打印名称"

或者,您也可以将代码更改为以下内容:

Alternatively you could also change your code to something like this:

ref.child("PGroups").observeSingleEventOfType(.Value, withBlock: { (snapshot) in
    let snapshotValue = snapshot.value as! [String: Any]
    let groupName = snapshotValue["GroupName"] as! String
    print(groupName)
})

这篇关于在不可选的情况下获取可选的Firebase数据库值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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