Skip to content

Instantly share code, notes, and snippets.

@ronaldmannak
Last active December 11, 2015 02:48
Show Gist options
  • Save ronaldmannak/4532707 to your computer and use it in GitHub Desktop.
Save ronaldmannak/4532707 to your computer and use it in GitHub Desktop.
Making NSNull behave like nil. See blog post ronaldmannak.github.com/2013/01/14/nsnull.html
//
// NSNull+behave.m
//
// Created by Ronald Mannak on 1/2/13.
// Copyright (c) 2013 Ronald Mannak. All rights reserved.
//
@implementation NSNull (catch)
- (NSMethodSignature *)methodSignatureForSelector:(SEL)aSelector {
// If NSNull doesn't respond to aSelector, signature will be nil and a new signature for an empty method
// will be created and returned
NSMethodSignature *signature = [super methodSignatureForSelector:aSelector];
if (!signature) {
// Note: "@:" are (id)self and (SEL)_cmd
signature = [NSMethodSignature signatureWithObjCTypes:"@:"];
}
return signature;
}
- (void)forwardInvocation:(NSInvocation *)anInvocation
{
// Called if NSNull received a message to a non-existent method
// Reroute the message to nil
[anInvocation invokeWithTarget:nil];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment