我将Swift 2项目转换为Swift 3.现在我使用Firebase获取数据,并且我已经拥有此功能:
self.ref.child("languages").observeSingleEvent(of: .value, with: { (snapshot) in
let languages = snapshot.value as! [String : AnyObject]
var languageList = [Language]()
for (key, value) in languages {
let tempLanguage = Language()
tempLanguage.objectId = key
tempLanguage.code = value["code"] as! String
tempLanguage.desc = value["fulldesc"] as! String
languageList.append(tempLanguage)
}
handler(success: languageList, failure: nil)
}) { (error) in
handler(nil, error)
}
这在swift 2中很好,但现在在Swift 3中,我在行中出现错误。错误如下:
无法转换类型的值(AnyObject?) - >无效到预期的参数类型((错误) - > Void)?
我删除了这个功能并使用自动完成功能,我再次写了它并且它也是同样的问题。
答案 0 :(得分:1)
只需将您的代码更改为: -
FIRDatabase.database().reference().child("languages").observeSingleEvent(of: .value, with: { (snapshot) in
let languages = snapshot.value as! [String : AnyObject]
var languageList = [Language]()
for (key, value) in languages {
let tempLanguage = Language()
tempLanguage.objectId = key
tempLanguage.code = value["code"] as! String
tempLanguage.desc = value["fulldesc"] as! String
languageList.append(tempLanguage)
}
handler(success: languageList, failure: nil)
}, withCancel: {(error) in
handler(nil, error)
})