Skip to content

Instantly share code, notes, and snippets.

@janodev
Last active Apr 16, 2018
Embed
What would you like to do?
Capture ObjC exceptions. From https://stackoverflow.com/a/36454808/412916
#import <Foundation/Foundation.h>
@interface ObjC: NSObject
+ (BOOL)catchException:(void(^)(void))tryBlock error:(__autoreleasing NSError **)error;
@end
#import "ObjC.h"
@implementation ObjC
+ (BOOL)catchException:(void(^)(void))tryBlock error:(__autoreleasing NSError **)error {
@try {
tryBlock();
return YES;
}
@catch (NSException *exception) {
*error = [[NSError alloc] initWithDomain:exception.name code:0 userInfo:exception.userInfo];
return NO;
}
}
@end
/// Return a storyboard for the given name and bundle. Log a console error if not found.
private static func createStoryboard(name: String, bundle: Bundle) -> UIStoryboard? {
var storyboard: UIStoryboard? = nil
do {
try ObjC.catchException {
storyboard = UIStoryboard(name: name, bundle: bundle)
}
} catch {
print("🚨 Failed to create storyboard \(name): \(error)")
}
return storyboard
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment