Skip to content

Instantly share code, notes, and snippets.

@Moligaloo
Created January 9, 2016 03:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Moligaloo/84c8a5265af3a328cf94 to your computer and use it in GitHub Desktop.
Save Moligaloo/84c8a5265af3a328cf94 to your computer and use it in GitHub Desktop.
help iOS developer to breakpoint when NSDictionary construction failed because of nil object
#import <objc/runtime.h>
#import <objc/message.h>
// copied from http://stackoverflow.com/questions/17346046/advice-on-how-to-catch-attempt-to-insert-nil-object-from-a-device-needed
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wundeclared-selector"
static id safe_initWithObjects(id self, SEL _cmd, const id objects[], const id <NSCopying> keys[], NSUInteger count) {
id orignialResult = nil;
@try {
orignialResult = objc_msgSend(self, @selector(safe_initWithObjects:forKeys:count:), objects, keys, count);
}
@catch (NSException *exception) {
NSLog(@"BUSTED!"); // put breakpoint here
}
return orignialResult;
}
void EnableHelpfulBreakpoints(){
Class target = NSClassFromString(@"__NSPlaceholderDictionary");
class_addMethod(target, @selector(safe_initWithObjects:forKeys:count:), (IMP)&safe_initWithObjects, "@@:**L");
Method m1 = class_getInstanceMethod(target, @selector(safe_initWithObjects:forKeys:count:));
Method m2 = class_getInstanceMethod(target, @selector(initWithObjects:forKeys:count:));
method_exchangeImplementations(m1, m2);
}
#pragma clang diagnostic pop
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment