Created
January 9, 2016 03:32
-
-
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
This file contains 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/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