Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Rails' nil?, for Objective-C
#import <Foundation/Foundation.h>
@interface NSObject (NSNullCheck)
- (BOOL)isNull;
@implementation NSObject (NSNullCheck)
- (BOOL)isNull { return NO; }
@implementation NSNull (NSNullCheck)
- (BOOL)isNull { return YES; }
int main(int argc, char **argv)
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSString *s = @"My string";
NSNull *n = [NSNull null];
NSLog(@"s is null? %@ (should be NO)", [s isNull] ? @"YES" : @"NO");
NSLog(@"n is null? %@ (should be YES)", [n isNull] ? @"YES" : @"NO");
[pool release];
return 0;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment