SwiftyNSException
SwiftyNSException 为 NSException
添加对 Error
协议的遵循,并提供一个包装函数,它捕获 Objective-C 异常并将其重新抛出为 Swift,这样您就可以使用 do-catch
块来捕获它。
要求
- Swift 5
- iOS 8.0+
- macOS 10.9+
- tvOS 9.0+
- watchOS 2.0+
安装
CocoaPods
要在 CocoaPods 中添加最新版本,请将以下内容添加到您的 Podfile
:
use_frameworks!
pod 'SwiftyNSException'
对于最新开发版本
use_frameworks!
pod 'SwiftyHaru', :git => 'https://github.com/broadwaylamb/SwiftyNSException.git', :branch => 'master'
使用说明
使用通用函数 handle(_:)
来封装可能抛出 NSException 的代码。
import SwiftyNSException
do {
let result = try handle { () -> String in
// Some invocation that can throw an NSException
return "OK"
}
// The type of `result` is `String`.
} catch let exception as NSException {
// Handle the exception
} catch {
// Handle some other error thrown.
}