Skip to content

Instantly share code, notes, and snippets.

@ericbroska
Last active March 31, 2016 04:41
Show Gist options
  • Save ericbroska/4482642 to your computer and use it in GitHub Desktop.
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.
/*
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