Last active
August 29, 2015 14:18
-
-
Save warpling/829e93a1357e9ef7f282 to your computer and use it in GitHub Desktop.
DictionaryProperties
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
// | |
// DictionaryProperties.h | |
// | |
// Created by KlausCPH on 3/1/13. | |
// Source: http://stackoverflow.com/a/15167563/522498 | |
// | |
@interface DictionaryProperties : NSObject{ | |
NSMutableDictionary* _backingDict; | |
} | |
@property (nonatomic, strong) NSMutableDictionary* backingDict; | |
+ (DictionaryProperties*) allocWithDictionary:(NSDictionary*)dict; | |
@end |
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
// | |
// DictionaryProperties.m | |
// | |
// Created by KlausCPH on 3/1/13. | |
// Source: http://stackoverflow.com/a/15167563/522498 | |
// | |
#import "DictionaryProperties.h" | |
@implementation DictionaryProperties | |
@synthesize backingDict = _backingDict; | |
- (id) initWithDictionary:(NSDictionary*)dict { | |
if (self) { | |
if ([dict isKindOfClass:[NSMutableDictionary class]]) { | |
self.backingDict = (id)dict; | |
} else { | |
self.backingDict = [[NSMutableDictionary alloc] initWithDictionary:dict]; | |
} | |
} | |
return self; | |
} | |
+ (DictionaryProperties*) allocWithDictionary:(NSDictionary*)dict { | |
return [[DictionaryProperties alloc] initWithDictionary:dict]; | |
} | |
- (void)forwardInvocation:(NSInvocation *)invocation | |
{ | |
NSString* key = NSStringFromSelector(invocation.selector); | |
invocation.selector = @selector(objectForKey:); | |
[invocation setArgument:&key atIndex:2]; | |
if ([self.backingDict objectForKey:key]) { | |
[invocation invokeWithTarget:self.backingDict]; | |
} else { | |
[self doesNotRecognizeSelector:invocation.selector]; | |
} | |
} | |
- (NSMethodSignature *)methodSignatureForSelector:(SEL)aSelector{ | |
return [self.backingDict methodSignatureForSelector:@selector(objectForKey:)]; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment