PromiseKit 6.0结构是否正确?因为我在运行应用程序时收到错误:(
这是图像:
//Promise block
firstly{
removePreviusSearch(text)
}.then {(finished) -> Promise<AI> in
aiService.getAi()
}.done {(ai) -> Void in
self.updateResults(ai)
}.catch { (error) in
//catch error
}
}
// MARK : Remove previus search
func removePreviusSearch(_ newText: String) -> Promise<Bool> {
return Promise { seal in
UIView.animate(withDuration: 0.5, animations:{
self.topLabel.alpha = 0
self.mainText.alpha = 0
self.resultsView.alpha = 0
self.textField.text = ""
}, completion: { (finished: Bool) in
UIView.animate(withDuration: 0.5) {
self.topLabel.alpha = 1
self.mainText.alpha = 1
}
seal.reject(finished as! Error)
self.topLabel.text = "user says".uppercased()
self.mainText.text = newText
self.setLabel(self.cityLabel)
self.setLabel(self.streetLabel)
self.setLabel(self.countryLabel)
self.setLabel(self.dateLabel)
self.setLabel(self.speechLabel)
self.setLabel(self.itentLabel)
self.setLabel(self.conditionLabel)
self.setLabel(self.outfitLabel)
self.setLabel(self.scoreLabel)
})
}
}
答案 0 :(得分:1)
completion: { (finished: Bool) in
...
seal.reject(finished as! Error) // casting error
...
首先,已完成是一个布尔值,您无法强制将其强制转换为错误。如果您想拒绝错误,则应该如下所示初始化错误。
let error = NSError(domain: "some information", code: 0, userInfo: nil) as Error
reject(error)
其次,我不知道你为什么要在代码中间拒绝,它会破坏整个承诺链。在所有事情都正确完成后,您应该履行承诺。
}, completion: { (finished: Bool) in
UIView.animate(withDuration: 0.5) {
self.topLabel.alpha = 1
self.mainText.alpha = 1
}
self.topLabel.text = "user says".uppercased()
self.mainText.text = newText
self.setLabel(self.cityLabel)
self.setLabel(self.streetLabel)
self.setLabel(self.countryLabel)
self.setLabel(self.dateLabel)
self.setLabel(self.speechLabel)
self.setLabel(self.itentLabel)
self.setLabel(self.conditionLabel)
self.setLabel(self.outfitLabel)
self.setLabel(self.scoreLabel)
// fulfill your promise right here
})
...