Skip to content

broadwaylamb/SwiftyNSException

Repository files navigation

SwiftyNSException

Build Status codecov Language Platform Cocoapods

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 'SwiftyNSException', :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.

}

About

A tiny library that lets you catch Objective-C NSExceptions right in your Swift code.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 4

  •  
  •  
  •  
  •