PromiseKit ios Swift 闭包返回类型问题
Posted
技术标签:
【中文标题】PromiseKit ios Swift 闭包返回类型问题【英文标题】:PromiseKit ios Swift closure return type issue 【发布时间】:2019-04-09 06:39:01 【问题描述】:我在我的应用程序中使用 Promise Kit。我是新来的承诺工具包。我需要知道这个闭包是如何工作的。因此,如果有人可以用示例来解释这件事,那将非常有帮助。
我面临的问题是,虽然我返回的是预期值,但我收到一条错误消息,指出无法转换类型的返回表达式....
我想也许我在这里理解错了。
这就是我使用承诺链的方式。
Glyph().glyphs(book: self.downloadingBook!._id)
.then glyphs -> Promise<[Glyph]> in
self.setProgress(progress: 0.07)
return Glyph().saveGlyphs(data: glyphs)
.then lines -> Promise<[Line]> in
self.setProgress(progress: 0.14)
return Line().lines(book: self.downloadingBook._id)
.then lines -> Promise<[Line]> in
self.setProgress(progress: 0.21)
return Line().saveLines(data: lines)
.then surahs -> Promise<[SurahGlyph]> in
self.setProgress(progress: 0.28)
return SurahGlyph().surahs(book: self.downloadingBook._id)
.then surahs -> Promise<[SurahGlyph]> in
self.setProgress(progress: 0.35)
return SurahGlyph().saveSurahGlyphs(data: surahs)
.then surahs -> Promise<[SurahGlyph]> in
self.setProgress(progress: 0.42)
return Translator().translators()
.then data -> Promise<[Translator]> in
self.setProgress(progress: 0.49)
return Translator().saveTranslators(translators: data)
.then data -> Promise<[Translator]> in
self.setProgress(progress: 0.56)
return Surah().getSurahs(translatorId: "ar.tazweed")
.then surahs -> Promise<[Surah]> in
self.setProgress(progress: 0.63)
return Surah().saveSurahs(surahs: surahs)
.then surahs -> Promise<[Surah]> in
self.setProgress(progress: 0.70)
Ayah().ayahs(page: 1)
.then ayahs -> Promise<[Ayah]> in
self.setProgress(progress: 0.77)
Ayah().saveAyahs(ayahs: ayahs)
return Promise.value(ayahs)
.done _ in
self.setProgress(progress: 1)
self.statusLabel.text = "Completed"
self.reloadBook()
.catch error in
print(error)
这就是我从函数中返回的方式:
func translators() -> Promise<[Translator]>
do
let realm = try Realm()
let translatorsInRealm = realm.objects(Translator.self)
if translatorsInRealm.count != 0
Promise.value(Array(translatorsInRealm))
else
return Promise<[Translator]> seal in
let handler = RequestHandler()
let session = RequestHandler.alamofireManager
session.request(API.Quran.translatorsURL, method: .get, parameters: nil,
encoding: URLEncoding.default, headers: nil,
interceptor: handler).validate().response (response) in
if response.result.isSuccess
do
let linesFromServer = try JSONDecoder().decode([Translator].self, from: response.data!)
seal.fulfill(linesFromServer)
catch
seal.reject(error)
else
seal.reject(response.error!)
catch
print("Unexpected error: \(error).")
return Promise.value([])
我遇到的错误
【问题讨论】:
您需要返回正确的承诺类型。第一个错误中的Promise<[Translator]>
而不是Promise<[SurahGlyph]>
。同样修复另一个。您也忘记返回突出显示Warning
的承诺。
你能解释我哪里错了吗?我添加了 Translator().translators() 函数。在哪里你可以看到我正在返回 Promise 并且在调用函数之后,在那部分中我期待数据 -> Promise.. 但它向我显示一个错误,说我期待 Promise
【参考方案1】:
promise 值类型不匹配。修正后的链条会是这个样子,
Glyph().glyphs(book: self.downloadingBook!._id)
.then glyphs -> Promise<[Glyph]> in
self.setProgress(progress: 0.07)
return Glyph().saveGlyphs(data: glyphs)
.then lines -> Promise<[Line]> in
self.setProgress(progress: 0.14)
return Line().lines(book: self.downloadingBook._id)
.then lines -> Promise<[Line]> in
self.setProgress(progress: 0.21)
return Line().saveLines(data: lines)
.then surahs -> Promise<[SurahGlyph]> in
self.setProgress(progress: 0.28)
return SurahGlyph().surahs(book: self.downloadingBook._id)
.then surahs -> Promise<[SurahGlyph]> in
self.setProgress(progress: 0.35)
return SurahGlyph().saveSurahGlyphs(data: surahs)
.then surahs -> Promise<[Translator]> in
self.setProgress(progress: 0.42)
return Translator().translators()
.then data -> Promise<[Translator]> in
self.setProgress(progress: 0.49)
return Translator().saveTranslators(translators: data)
.then data -> Promise<[Surah]> in
self.setProgress(progress: 0.56)
return Surah().getSurahs(translatorId: "ar.tazweed")
.then surahs -> Promise<[Surah]> in
self.setProgress(progress: 0.63)
return Surah().saveSurahs(surahs: surahs)
.then surahs -> Promise<[Ayah]> in
self.setProgress(progress: 0.70)
return Ayah().ayahs(page: 1)
.then ayahs -> Promise<[Ayah]> in
self.setProgress(progress: 0.77)
Ayah().saveAyahs(ayahs: ayahs)
return Promise.value(ayahs)
.done _ in
self.setProgress(progress: 1)
self.statusLabel.text = "Completed"
self.reloadBook()
.catch error in
print(error)
【讨论】:
以上是关于PromiseKit ios Swift 闭包返回类型问题的主要内容,如果未能解决你的问题,请参考以下文章
使用 PromiseKit 6 在 Swift 4.2 中缓存
如果一个已经在运行的 PromiseKit 承诺返回一个未决的承诺的最佳 Swift 模式是啥?