Skip to content

Instantly share code, notes, and snippets.

@mdippery
Created November 19, 2010 22:40
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 mdippery/707337 to your computer and use it in GitHub Desktop.
Save mdippery/707337 to your computer and use it in GitHub Desktop.
Rails' nil?, for Objective-C
#import <Foundation/Foundation.h>
@interface NSObject (NSNullCheck)
- (BOOL)isNull;
@end
@implementation NSObject (NSNullCheck)
- (BOOL)isNull { return NO; }
@end
@implementation NSNull (NSNullCheck)
- (BOOL)isNull { return YES; }
@end
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