swift 2 : Error handling: throw: How to pass an object with an error?

标签 swift error-handling swift2

Swift2抛错时如何传递userInfo引用对象?

像这样的东西会很好:

guard let refIDString = memberXMLElement.attributes["ref"] else {
     throw OSMVectorMapDescriptionError.ElementDoesntContainRequiredAttributes(userInfo:memberXMLElement)
}

然后:

catch OSMVectorMapDescriptionError.ElementDoesntContainRequiredAttributes {
  (userInfo:AnyObject) in
      //do stuff                 
 }

但是错误是枚举,我们确实可以像这里那样指定,但是如何捕捉呢?

public enum OSMVectorMapDescriptionError:ErrorType {
    case ElementDoesntContainRequiredAttributes(userInfo:AnyObject)
}

最佳答案

假设你不需要someLevel,你可以定义

public enum OSMVectorMapDescriptionError:ErrorType {
   case ElementDoesntContainRequiredAttributes(userInfo: ...)
}

保持警惕

guard let refIDString = memberXMLElement.attributes["ref"] else {
    throw OSMVectorMapDescriptionError.
              ElementDoesntContainRequiredAttributes(userInfo:memberXMLElement)
}

catch OSMVectorMapDescriptionError.
          ElementDoesntContainRequiredAttributes(let userInfo) {
  //do stuff using userInfo              
}

关于 swift 2 : Error handling: throw: How to pass an object with an error?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31559364/

相关文章:

ios - 如何使用 swift ios 以全屏模式播放视频?

ios - Swift 我无法隐藏分段控件

error-handling - Apache Camel 中doFinally block 的错误处理

MATLAB:VideoReader 错误检查

swift - 为什么没有为可选数组定义 Equatable

swift - WKWebView无法拖动Element

ios - 单击按钮并更改其图像时的 UITableView Swift 4

python - 如何确定语料库中的哪些文本包含Python中的NLTK套件生成的错误?

swift - CMPedometer 在 Swift 2.0 中出现错误

ios - 核心数据在我删除它并重新启动我的应用程序后仍然存在