Skip to content

Instantly share code, notes, and snippets.

@bryanluby
Last active August 29, 2015 13:56
Show Gist options
  • Save bryanluby/9159185 to your computer and use it in GitHub Desktop.
Save bryanluby/9159185 to your computer and use it in GitHub Desktop.
Create arbitrary key-value pairs for an object (similar to kvc compliant container classes like CALayer).
@interface CustomObject ()
@property (strong, nonatomic) NSMutableDictionary *arbitraryKeyValuePairs;
@end
@implementation CustomObject
- (void)setValue:(id)value forUndefinedKey:(NSString *)key
{
if (!self.arbitraryKeyValuePairs) {
self.arbitraryKeyValuePairs = [NSMutableDictionary dictionary];
}
self.arbitraryKeyValuePairs[key] = value;
}
- (id)valueForUndefinedKey:(NSString *)key
{
NSArray *allKeys = [self.arbitraryKeyValuePairs allKeys];
if ([allKeys containsObject:key]) {
return self.arbitraryKeyValuePairs[key];
} else {
return [super valueForUndefinedKey:key];
}
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment