Skip to content

Instantly share code, notes, and snippets.

@marty-suzuki
Created October 6, 2016 15:13
Show Gist options
  • Save marty-suzuki/8054a9048a50fa8aa319dcf358cc0832 to your computer and use it in GitHub Desktop.
Save marty-suzuki/8054a9048a50fa8aa319dcf358cc0832 to your computer and use it in GitHub Desktop.
@implementation NSObject (Error)
- (void)removeObserver:(NSObject * _Nonnull)observer forKeyPath:(NSString * _Nonnull)keyPath error:(NSError * _Nullable __autoreleasing * _Nullable)error {
@try {
[self removeObserver:observer forKeyPath:keyPath];
} @catch (NSException *exception) {
NSMutableDictionary *userInfo = nil;
if (exception.userInfo == nil) {
userInfo = [NSMutableDictionary dictionaryWithCapacity:0];
} else {
userInfo = exception.userInfo.mutableCopy;
}
userInfo[NSLocalizedDescriptionKey] = exception.reason;
*error = [NSError errorWithDomain:exception.name code:-9999 userInfo:userInfo];
}
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment