Last active
March 31, 2016 04:41
-
-
Save ericbroska/4482642 to your computer and use it in GitHub Desktop.
The simple way to make NSNull act as nil: to respond with `0` (==`nil`) to any message.
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
/* | |
clang -o null -framework Foundation null.m | |
ericbroska | |
*/ | |
#import <Foundation/Foundation.h> | |
#include <objc/objc-class.h> | |
#include <objc/runtime.h> | |
int main(int argc, char const *argv[]) | |
{ | |
@autoreleasepool { | |
/* voids */ | |
[[NSNull null] performSelector: @selector(magic)]; | |
[[NSNull null] start]; | |
[[NSNull null] addObserver: nil | |
forKeyPath: @"kNullWeirdKeyPath" | |
options: 0 | |
context: [NSString stringWithString: @"something"]]; | |
NSLog(@"%@ : %lu", @"NSUInteger", [[NSNull null] count]); | |
NSLog(@"%@ : %d", @"BOOL/Integer", [[NSNull null] isLessThan: @3.14 ]); | |
NSLog(@"%@ : %c", @"Char", [[NSNull null] characterAtIndex: 51] ?: '?'); | |
NSLog(@"%@ : %@", @"Object", [[NSNull null] objectForKey: @"key"]); | |
} | |
return 0; | |
} | |
int _placeholder(id self, SEL selector, ...) | |
{ | |
return 0; | |
} | |
@implementation NSNull (TryToFailMe) | |
+ (BOOL)resolveInstanceMethod:(SEL)aSEL | |
{ | |
return class_addMethod([self class], aSEL, (IMP)_placeholder, "i@:?"); | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment