Last active
April 16, 2018 09:18
-
-
Save janodev/f508ca499a8f276d60064db8631bd9ce to your computer and use it in GitHub Desktop.
Capture ObjC exceptions. From https://stackoverflow.com/a/36454808/412916
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import <Foundation/Foundation.h> | |
@interface ObjC: NSObject | |
+ (BOOL)catchException:(void(^)(void))tryBlock error:(__autoreleasing NSError **)error; | |
@end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// 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