Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save alexrozanski/307568 to your computer and use it in GitHub Desktop.
Save alexrozanski/307568 to your computer and use it in GitHub Desktop.
NSMutableDictionary category to handle setting nil as an object for a key, adding [NSNull null] instead.
#import <Cocoa/Cocoa.h>
@interface NSMutableDictionary (PXDictionaryAdditions)
- (BOOL)setObjectOrNull:(id)anObject forKey:(id)aKey;
@end
#import "NSMutableDictionary+PXDictionaryAdditions.h"
@implementation NSMutableDictionary (PXDictionaryAdditions)
//Returns NO if `anObject` is nil; can be used by the sender of the message or ignored if it is irrelevant.
- (BOOL)setObjectOrNull:(id)anObject forKey:(id)aKey
{
if(anObject!=nil) {
[self setObject:anObject forKey:aKey];
return YES;
}
else {
[self setObject:[NSNull null] forKey:aKey];
return NO;
}
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment