在方法签名中使用特定错误类型抛出快速错误

时间:2015-11-04 18:56:53

标签: ios swift exception-handling

使用2.1中引入的新swift error handling是否可以指定一个方法将抛出的给定ErrorType

e.g。 class func nextOrderDate() throws OrderError -> NSDate {...}

1 个答案:

答案 0 :(得分:4)

在Swift中,您可以捕获特定类型,而不是抛出特定类型,如下所示:

do {
   let date = try nextOrderDate() 
} catch let error as OrderError {
   print("OrderError")
} catch {
   print("other error")
}

我多次看过的解决方法是返回错误(通常在完成块中看到):

class func nextOrderDate() -> (NSDate?, OrderError?)