joesus / NoExceptions

A utility to provide the ability to handle ObjectiveC exceptions

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool







NoExceptions

This is a library that provides the ability for Swift code to catch an NSException.

This is useful since the standard try and catch mechanism provided by Swift will not capture an NSException thrown from Objective-C.

Syntax

Presuming an Objective-C method that can throw an instance of NSException as shown in the example of MyClass below, the second example would allow for catching the exception in a Swift Error type that wraps it.

@interface MyClass: NSObject

- (void)throwingMethod;
- (id)throwingGetter;

@end
try? trapping(trappable: myObject.throwingMethod)
try? trapping { myObject.throwingMethod() }
let foo = try? trapping { myObject.throwingGetter() }
try? trapping { let bar = myObject.throwingGetter() }
let baz = try? trapping(trappable: myObject.throwingGetter)

do {
    try trapping { myObject.throwingMethod() }
    let variable = try trapping { myObject.throwingGetter() }
    try trapping { let bar = myObject.throwingGetter() }
}
catch error as NSExceptionError {
    /// handle error containing exception
}

About

A utility to provide the ability to handle ObjectiveC exceptions

License:MIT License


Languages

Language:Objective-C 56.3%Language:Swift 32.3%Language:Ruby 11.4%