gpt4 book ai didi

swift - 枚举返回对象(NSError)

转载 作者:行者123 更新时间:2023-11-30 10:18:15 27 4
gpt4 key购买 nike

这个相对简单的enum,编译得很好,但是......

public protocol AuthKitErrorProtocol {
var error: NSError { get }
}

enum AuthKitError: AuthKitErrorProtocol {
case SignInFailedInvalidUsernamePassword
case SignInFailedInvalidSecondFactor

var error: NSError {
let (codeK: Int,
userInfoK: Dictionary<String, String>
) = {
switch self {
case .SignInFailedInvalidUsernamePassword:
return (1000,
[ NSLocalizedDescriptionKey: "Operation failed.",
NSLocalizedFailureReasonErrorKey: "Invalid usename or password",
NSLocalizedRecoverySuggestionErrorKey: "Please try again" ]
)

case .SignInFailedInvalidSecondFactor:
return (1001,
[ NSLocalizedDescriptionKey: "Operation failed.",
NSLocalizedFailureReasonErrorKey: "Invalid second factor code",
NSLocalizedRecoverySuggestionErrorKey: "Please try again" ]
)

default:
return (-999,
[ NSLocalizedDescriptionKey: "Operation failed.",
NSLocalizedFailureReasonErrorKey: "Unknown reason",
NSLocalizedRecoverySuggestionErrorKey: "Please call customer support" ]
)

}
}()

return NSError(domain: kAuthKitErrorDomain, code: codeK, userInfo: userInfoK)
}
}

但是当尝试从中提取时

var err: NSError  = AuthKitError.SignInFailedInvalidUsernamePassword
println("error is : \(err)")

它失败了,说:

AuthKitError is not convertible to NSError

任何想法将不胜感激!感谢您抽出时间。

最佳答案

error 是枚举的实例属性,AuthKitError.SignInFailedInvalidUsernamePassword 是枚举的实例,因此您必须显式读取其值:

var err: NSError  = AuthKitError.SignInFailedInvalidUsernamePassword.error
^^^^^^

关于swift - 枚举返回对象(NSError),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28952748/

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