Skip to content

Instantly share code, notes, and snippets.

@tternes
Created August 31, 2012 06:56
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tternes/3549736 to your computer and use it in GitHub Desktop.
Save tternes/3549736 to your computer and use it in GitHub Desktop.
// .h
@interface NSNull (null)
+ (void) setWarnsOnNullAccess:(BOOL) warnsOnNullAccess;
- (BOOL) boolValue;
- (char) charValue;
- (NSDecimal) decimalValue;
- (double) doubleValue;
- (float) floatValue;
- (int) intValue;
- (NSInteger) integerValue;
- (long long) longLongValue;
- (long) longValue;
- (short) shortValue;
- (unsigned char) unsignedCharValue;
- (NSUInteger) unsignedIntegerValue;
- (unsigned int) unsignedIntValue;
- (unsigned long long) unsignedLongLongValue;
- (unsigned long) unsignedLongValue;
- (unsigned short) unsignedShortValue;
- (id) valueForKey:(NSString *) key;
- (id) objectAtIndex:(NSUInteger) index;
- (id) objectAtIndexedSubscript:(NSUInteger) index;
- (NSUInteger) count;
- (id) objectForKey:(id) key;
- (id) objectForKeyedSubscript:(id) key;
- (NSUInteger) length;
@end
// .m
#define PrintAccessLog(selector) \
if (warnsOnNullAccess) { \
NSLog(@"Attempting to access null value from \"%@\": %@", NSStringFromSelector(selector), [NSThread callStackSymbols]); \
}
@implementation NSNull (null)
static BOOL warnsOnNullAccess = NO;
+ (void) setWarnsOnNullAccess:(BOOL) shouldWarnOnNullAccess {
warnsOnNullAccess = shouldWarnOnNullAccess;
}
#pragma mark -
- (BOOL) boolValue {
PrintAccessLog(_cmd)
return NO;
}
- (char) charValue {
PrintAccessLog(_cmd)
return '\0';
}
- (NSDecimal) decimalValue {
PrintAccessLog(_cmd)
return [[NSDecimalNumber zero] decimalValue];
}
- (double) doubleValue {
PrintAccessLog(_cmd)
return 0.0;
}
- (float) floatValue {
PrintAccessLog(_cmd)
return 0.0;
}
- (int) intValue {
PrintAccessLog(_cmd)
return 0;
}
- (NSInteger) integerValue {
PrintAccessLog(_cmd)
return 0;
}
- (long long) longLongValue {
PrintAccessLog(_cmd)
return 0;
}
- (long) longValue {
PrintAccessLog(_cmd)
return 0;
}
- (short) shortValue {
PrintAccessLog(_cmd)
return 0;
}
- (unsigned char) unsignedCharValue {
PrintAccessLog(_cmd)
return '\0';
}
- (NSUInteger) unsignedIntegerValue {
PrintAccessLog(_cmd)
return 0;
}
- (unsigned int) unsignedIntValue {
PrintAccessLog(_cmd)
return 0;
}
- (unsigned long long) unsignedLongLongValue {
PrintAccessLog(_cmd)
return 0;
}
- (unsigned long) unsignedLongValue {
PrintAccessLog(_cmd)
return 0;
}
- (unsigned short) unsignedShortValue {
PrintAccessLog(_cmd)
return 0;
}
- (id) valueForKey:(NSString *) key {
PrintAccessLog(_cmd)
return nil;
}
- (id) objectAtIndex:(NSUInteger) index {
PrintAccessLog(_cmd)
return nil;
}
- (id) objectAtIndexedSubscript:(NSUInteger) index {
PrintAccessLog(_cmd)
return nil;
}
- (NSUInteger) count {
PrintAccessLog(_cmd)
return 0;
}
- (id) objectForKey:(id) key {
PrintAccessLog(_cmd)
return nil;
}
- (id) objectForKeyedSubscript:(id) key {
PrintAccessLog(_cmd)
return nil;
}
- (NSUInteger) length {
PrintAccessLog(_cmd)
return 0;
}
#pragma mark NSString Messages
- (NSUInteger) _fastCharacterContents {
PrintAccessLog(_cmd);
return 0;
}
- (void)getCharacters:(unichar *)buffer range:(NSRange)range {
PrintAccessLog(_cmd);
assert(range.length == 0);
assert(range.location == 0);
}
- (NSString *)substringFromIndex:(NSInteger)index {
PrintAccessLog(_cmd);
return nil;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment