gpt4 book ai didi

swift - 如何嵌套低级错误和高级错误?

转载 作者:行者123 更新时间:2023-11-30 13:43:44 25 4
gpt4 key购买 nike

我有一个自定义错误类型,用于处理低级 HTTP 问题:

enum APIClientError: ErrorType {
case NetworkError
...
}

在更高级别的层中,我有另一种错误类型:

enum SignInError: ErrorType {
case InvalidUser
...
}

我遇到的问题是那些 APIClientError 实例需要冒泡到更高级别的层,并且在我的函数中我需要返回一个 APIClientError ,也许一个 登录错误

如何声明这样的函数?我试过了

typealias LoginResult = Result<SuccessType, ErrorType>

但我不起作用(不支持“使用 ErrorType”作为符合协议(protocol)“ErrorType”的具体类型”)。

如何在 Swift 中嵌套来自不同层的错误类型?

最佳答案

我也为这个问题摸不着头脑。我能想到的唯一方法是这样的:

 enum APIClientError: ErrorType {
case NetworkError
...
}


enum SignInError: ErrorType {
case InvalidUser
//note, the name of the case can't be the same as the type of the error due to compiler errors
case APIError(APIClientError)
}

这样错误就会冒出来。唯一烦人的事情是通过处理错误的代码来展开多层错误嵌套。

关于swift - 如何嵌套低级错误和高级错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35235661/

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