gpt4 book ai didi

ios - 如何使用 swift-4 Promises then, done, catch 等 block

转载 作者:可可西里 更新时间:2023-10-31 23:55:28 31 4
gpt4 key购买 nike

我想了解 swift-4 中的 promise 。如何使用多个 then 语句和 done、catch block 。

在这里,我试图从 promise 中获取值(value)。但我收到错误。有人可以帮助我理解 promise 吗?

这是我的代码。

import UIKit
import PromiseKit

struct User {
var firstname : String?
var lastname : String?
}

struct APIError {
var message : String?
}

class ViewController : UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let userPromise : Promise = self.getUserDetails()
userPromise.then { user -> Void in
//print(user.f)
}
}

func getUserDetails()->Promise<User> {
return Promise<User> { resolve in
let user = User(firstname: "Scot", lastname: "dem")
if ((user.firstname?.count) != nil) {
resolve.fulfill(user)
} else {
let error = APIError(message: "User not valid")
resolve.reject(error as! Error)
}
}
}
}

获得用户详细信息后,我想创建一个依赖于 userPromise 的大写全名 promise 。

我想使用多个 then, done, finally block 。只是想了解用法。

Why I'm getting an error here when we use userPromise.then { user -> Void in

我应该在方 block 内给出什么

最佳答案

在 PromiseKit 6 中,then 不能再返回 Void。这主要是由于 Swift 4 中的 tuplegate 问题。

引自 PromieKit 6 Release News

With PromiseKit our then did multiple things, and we relied on Swift to infer the correct then from context. However with multiple line thens it would fail to do this, and instead of telling you that the situation was ambiguous it would invent some other error. Often the dreaded cannot convert T to AnyPromise. We have a troubleshooting guide to combat this but I believe in tools that just work, and when you spend 4 years waiting for Swift to fix the issue and Swift doesn’t fix the issue, what do you do? We chose to find a solution at the higher level.

So we split then into then, done and map.

  • then is fed the previous promise value and requires you return a promise.
  • done is fed the previous promise value and returns a Void promise (which is 80% of chain usage)
  • map is fed the previous promise value and requires you return a non-promise, ie. a value.

因此 .then { (user) -> Void in 不再有效,这就是您收到错误的原因。
它现在被设计成像这样返回一个 Thenable:

userPromise.then { user -> Promise<User> in
//return yet another promise
}

用于返回 Void.then 现在是它自己的 .done
即:

userPromise.done { (user) in
print(user)
}

所以当你把它们混在一起时:

我们得到:

userPromise
.then { (user) -> Promise<User> in
//return another Promise
return self.uppercasePromise(on: user)
}
.done { (user) in
/*
Depending on your sequence, no more promises are left
and you should have a matured user object by now
*/
print(user)
}
.catch { (error) in
print(error)
}
.finally {
print("finally")
}

func uppercasePromise(on user: User) -> Promise<User> {
/*
Didn't understand your statement but do whatever you meant when you said:

"Once I get the user details I want to make a full name, "
uppercase promises which are dependent on userPromise."
*/
return Promise { seal in
seal.fulfill(user)
}
}

关于ios - 如何使用 swift-4 Promises then, done, catch 等 block ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50091362/

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