Swift 处理自定义错误,但有问题

Swift Handling Custom Error but something wrong

提问人:LiangOla 提问时间:9/11/2023 最后编辑:NerkyatorLiangOla 更新时间:9/11/2023 访问量:33

问:

这是我的自定义错误

public struct CSHttpError: Error {
    
    enum CSHttpErrorScene {
        case clientRequestError
        case customError
    }
    
    let code: Int?
    let kind: CSHttpErrorScene
    let description: String?
    
    init(code:Int?,kind:CSHttpErrorScene,description:String?) {
        self.code = code
        self.kind = kind
        self.description = description ?? ""
    }
}

当发布请求并获取空数据时 我想用CSHttpError抛出这个错误

let error = CSHttpError(code: -1, kind: .clientRequestError, description:"No Response Data")
throw error

但是当我发现此错误时,“描述”和“代码”丢失了

...
} catch let error as CSHttpError {
 print(error.description) // Optional("The operation couldn’t be completed. (VHK.CSHttpError error 1.)")
 print(error.kind) // .clientRequestError
 throw VHKError.network(error)
}

error.description 应为“无响应数据”????

我尝试调试它,但它不起作用

快速 错误处理

评论

0赞 Martin R 9/11/2023
可能有帮助: stackoverflow.com/q/39176196/1187415
2赞 Joakim Danielson 9/11/2023
这在 Xcode Playground 中是不可重现的

答: 暂无答案