简体   繁体   English

如何访问SwiftyDropbox错误的关联值?

[英]How can I access the associated values of SwiftyDropbox errors?

I've been working with SwiftyDropbox and I'm having a curious problem with errors. 我一直在使用SwiftyDropbox,我有一个奇怪的错误问题。 Specifically, I'm not sure how to manipulate errors in the closure callbacks provided after responses are received so that I can get at their associated values. 具体来说,我不确定如何处理收到响应后提供的闭包回调中的错误,以便我可以得到它们的关联值。

For instance, the completion handler for Dropbox.authorizedClient.filesListFolder provides a 例如,Dropbox.authorizedClient.filesListFolder的完成处理程序提供了一个

CallError<(Files.ListFolderError)>? 

to work with. 跟...共事。 How would I go about checking if it is a 我该如何检查它是否是一个

CallError.HTTPError

, so that I can get the HTTP error code out of it? ,以便我可以从中获取HTTP错误代码? Right now I'm just sucking that information out of the error's .description but that doesn't seem like the right way to do it. 现在我只是从错误的.description中吮吸那些信息,但这似乎不是正确的方法。

This is what I've tried. 这就是我尝试过的。 I suspect I'm failing to understand something with the generics involved. 我怀疑我对所涉及的仿制药没有理解。

Xcode截图

client.filesListFolder(path: "", recursive: false).response({ (listFolderResult, listFolderError) -> Void in

    switch listFolderError {
    case let .HTTPError(code, message, requestId):
        print("http error")
    default:
        print("not a http error")
    }

Enum case 'HTTPError' not found in type 'CallError?' 在'CallError?'类型中找不到枚举案例'HTTPError'

The problem here is that we're trying to switch on an optional. 这里的问题是我们正在尝试打开一个可选项。 This simpler example highlights the exact same problem: 这个更简单的例子突出了完全相同的问题:

在此输入图像描述

enum Foo {
    case a
    case b
}

let x: Foo? = nil

switch x {
case .a:
    print("a")
case .b:
    print("b")
}

Enum case 'a' not found in type 'Foo?' 在'Foo'类型中找不到枚举案'a'?

We can switch over optionals because Optional is itself an Enum , with two cases: None and Some(T) . 我们可以切换选项,因为Optional本身就是一个Enum ,有两种情况: NoneSome(T)

So when we're switching over an optional, Swift expects some code like this: 所以当我们切换一个可选项时,Swift需要这样的代码:

switch someOptional {
case .Some(someValue):
    print("do some things")
case .None:
    print("someOptional was nil")
}

But that's probably not necessarily particularly useful to use. 但这可能不一定特别有用。 We have an optional enum, and ultimately, if we dealt with our optional in a switch, we'd just have nested switch statements. 我们有一个可选的枚举,最后,如果我们在交换机中处理我们的可选项,我们只需要嵌套的switch语句。 Instead, we should deal with our optional in the normal Swift way of dealing with optionals: 相反,我们应该以正常的Swift方式处理我们的可选项处理选项:

if let error = listFolderError {
    switch error {
    case let .HTTPError(code, message, requestID):
        print("http error")
    default:
        print("some other error")
    }
} else {
    print("there was no error")
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM