SwiftyNSException
SwiftyNSException adds conformance to Error protocol for NSException and provides a wrapper function that
catches an Objective-C exception and rethrows it in Swift so you can catch it using the do-catch block.
Requirements
- Swift 5
- iOS 8.0+
- macOS 10.9+
- tvOS 9.0+
- watchOS 2.0+
Installation
CocoaPods
For the latest release in CocoaPods add the following to your Podfile:
use_frameworks!
pod 'SwiftyNSException'For the latest dev build:
use_frameworks!
pod 'SwiftyHaru', :git => 'https://github.com/broadwaylamb/SwiftyNSException.git', :branch => 'master'Usage
Use the generic function handle(_:) to wrap some code that can throw an 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.
}