我正在运行以下查询以从快照中获取值,但该值将作为可选值返回。
ref.child("PGroups").observeSingleEventOfType(.Value, withBlock: { (snapshot) in
let groupName = String(rest.childSnapshotForPath("/GroupName").value)
print(groupName)
})
我收到以下印刷声明:"Optional(Name)"
仅适用于:"Name"
答案 0 :(得分:2)
只需添加一个惊叹号即可告诉编译器打开可选项:
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)
})