gpt4 book ai didi

swift - PromiseKit 捕获多个参数

转载 作者:行者123 更新时间:2023-11-28 15:34:19 33 4
gpt4 key购买 nike

我正在将 PromiseKit 集成到我们当前的系统中,我需要链的 catch 部分使用 2 个参数。我希望我的 catch 使用 error, fetcher 而不仅仅是错误。执行此操作的最佳方法是什么?

infoPromise.then { objects in
print(objects)
}.catch { error in /* error, fetcher in */
print(error)
}

我考虑过将 fetcher 作为 NSError 上的 userInfo 字典的一部分,但我更喜欢将它作为一个单独的如果可能的话,争论。

编辑 - 更多信息:

这是我正在使用的适配器。 fetcher 在我现有系统的 onError 中返回。

- (AnyPromise *)fetchAll:(NSString *)entityName {
return [AnyPromise promiseWithAdapterBlock:^(PMKAdapter _Nonnull adapter) {
[self fetchAll:entityName onComplete:^(NSArray *results) {
adapter(results,nil);
} onError:^(ICSHTTPFetcher *fetcher, NSError *error) {
adapter(nil,error);
}];
}];
}

最佳答案

我在 Objective C 中使用的 promise 不多。但是如果你能够在 swift 中编写 fetchAll 函数,那么你可以将错误和 fetcher 包装在 Swift 错误中。下面是一个粗略的例子——它不会编译,只是给你一个想法。做一些更具体的 PromiseErrors 来指示实际错误可能会更好,而不是像我所做的那样然后不传递 NSError。

enum PromiseErrors: Error {
case fetchAll(NSError, ICSHTTPFetcher)
}

struct Test {
func fetchAll() -> Promise<Any> {
return fetchAll(entityName, onComplete: { results in
return Promise(value: results)
}, error: { fetcher, error in
return Promise(error: PromiseErrors.fetchAll(error, fetcher))
})
}

func test() {
infoPromise.then { objects in
print(objects)
}.catch { error in
if let case PromiseErrors.fetchAll(error, fetcher) = error {

}
}
}
}

关于swift - PromiseKit 捕获多个参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44441110/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com