这是我的代码:
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]
}
}
我无法理解以下错误:
Invalid conversion from throwing function of type
(_) throws -> ()to non-throwing function type(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]
关于ios - 错误 : Invalid conversion from throwing function of type '(_) throws -> ()' to non-throwing function type '(DataSnapshot) -> Void' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44599200/