Ios 在方法签名中抛出具有特定错误类型的swift错误
在2.1中引入了新的swift后,是否可以指定方法将抛出的给定Ios 在方法签名中抛出具有特定错误类型的swift错误,ios,swift,exception-handling,Ios,Swift,Exception Handling,在2.1中引入了新的swift后,是否可以指定方法将抛出的给定ErrorType e、 g。 class func nextOrderDate()在Swift中抛出OrderError->NSDate{…}而不是抛出特定类型,捕捉特定类型,如下所示: do { let date = try nextOrderDate() } catch let error as OrderError { print("OrderError") } catch { prin
ErrorType
e、 g。
class func nextOrderDate()在Swift中抛出OrderError->NSDate{…}
而不是抛出特定类型,捕捉特定类型,如下所示:
do {
let date = try nextOrderDate()
} catch let error as OrderError {
print("OrderError")
} catch {
print("other error")
}
我见过很多次的一种解决方法是返回错误(通常在完成块中看到):
SWIFT 5
您现在可以使用:
class func nextOrderDate() -> Result<NSDate, OrderError>
class func nextOrderDate()->Result
还没有。有一些详细的答案。旧答案,但由于Swift 5,工作区可以使用结果
类型。e、 g.结果
class func nextOrderDate() -> Result<NSDate, OrderError>