【问题标题】:Getting an optional firebase database value when it shouldn't be optional在不应该是可选的情况下获取可选的 firebase 数据库值
【发布时间】:2017-04-18 18:05:06
【问题描述】:

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

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

并且我得到以下打印声明:"Optional(Name)" 与刚才一样:"Name"

【问题讨论】:

  • 你为什么不认为这个值应该是可选的?

标签: ios swift firebase firebase-realtime-database


【解决方案1】:

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

print(groupName!) 应该打印 "Name"

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-03-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-19
    • 1970-01-01
    相关资源
    最近更新 更多