swift - Get error code from enum : error in swift 4.2
问题描述
I'm using Auth0 and when using biometrics, they are returning an error but the error code is incorrect.
they have a function that returns:
return callback(.touchFailed($0!), nil)
$0 is a LAError and .touchFailed is declared as
public enum CredentialsManagerError: Error {
case noCredentials
case noRefreshToken
case failedRefresh(Error)
case touchFailed(Error)
}
$0._code has a value of -3
but in the callback function the error._code is always equals to 1
How can I retrieved the actual value of -3?
解决方案
问题是您正在查看错误的错误对象。有两个错误对象到达,外部错误 ( .touchFailed
) 和包含在其中的内部错误。内部错误是您要检查的错误。但你不是在检查它!
要明白我的意思,看看这个错误的方法和正确的方法:
public enum CredentialsManagerError: Error {
case noCredentials
case noRefreshToken
case failedRefresh(Error)
case touchFailed(Error)
}
// let's make a `.touchFailed`
let innerError = NSError(domain: "yoho", code: -3, userInfo: nil)
let outerError = CredentialsManagerError.touchFailed(innerError)
// now let's examine them
// first, the wrong way
print(outerError._code) // 1, because it's the outer error
// now, the right way
if case let .touchFailed(what) = outerError {
print(what._code) // -3 <--!!!!
}
推荐阅读
- wordpress - 我对持久性页面链接有疑问
- aws-api-gateway - Terraform - 如何启用 API Gateway 执行日志记录?
- python - 是否有一种相对可靠的方法来检测当前脚本是否以 PYTHONSTARTUP 运行?
- java - Java 一个用于泊松分布的函数以获得分位数
- c# - C#中的Pramp反句面试题
- google-cloud-platform - 向 GCP 上的数据流作业添加更多工作人员
- ios - Google Maps iOS - 自定义信息窗口按钮不起作用
- node.js - nodejs和mongoose中的异步分离查询
- sql-server - 过滤器不适用于两个 where 子句
- python - 在Python中计算累积