![](/img/trans.png)
[英]Invalid conversion from throwing function of type '(_, _, _) throws -> ()' to non-throwing function type '(URLResponse?, Data?, Error?) -> Void
[英]Error: Invalid conversion from throwing function of type '(_) throws -> ()' to non-throwing function type '(DataSnapshot) -> Void'
这是我的代码:
func loadData() {
ref.child(currentUserID!).observe(.childAdded) {
(snapshot) in
let snapshotValue = try? snapshot.value as? [String: AnyObject]
if let item = try TableViewModel(id: snapshot.key, likeLabel: self.likeLabel, playLabelString: self.playLabelString, json: snapshotValue) {
self.items.append(item)
}
self.tableViewModel = self.items.reversed() as [TableViewModel]
}
}
我无法理解以下错误:
从
(_) throws -> ()
类型的throwing函数到(_) throws -> ()
无效转换到非throwing函数类型(DataSnapshot) -> Void
在以下行中:
ref.child(currentUserID!).observe(.childAdded) {
请帮忙。
您的主要问题是以下表达式:
if let item = try TableViewModel(...) {
try
命令表明您的Firebase完成关闭可能会引发错误,这种情况是您将关闭传递给的observe
API所无法预期的,因此,您遇到了编译错误。
好吧,顺便说一句,我相信您try?
运算符。 如果是这样,请尝试以下方法:
if let item = try? TableViewModel(...) {
或者简单地:
if let item = TableViewModel(...) {
如果TableViewModel
根本不引发任何错误(即,它只是一个失败的初始化器 )。
顺便说一句,您的snapshotValue
变量也可以使用一些帮助:
let snapshotValue = snapshot.value as! [String: AnyObject]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.