public
Created

Rails' nil?, for Objective-C

  • Download Gist
NSNullCheck.m
Objective-C
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
#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;
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.