Skip to content

Instantly share code, notes, and snippets.

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 Abizern/307982 to your computer and use it in GitHub Desktop.
Save Abizern/307982 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 <NSCopying>)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 <NSCopying>)aKey {
if(!anObject) {
[self setObject:[NSNull null] forKey:aKey];
return NO;
}
[self setObject:anObject forKey:aKey];
return YES;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment