Last active
August 29, 2015 13:56
-
-
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).
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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