Skip to content

Instantly share code, notes, and snippets.

@warpling
Last active August 29, 2015 14:18
Show Gist options
  • Save warpling/829e93a1357e9ef7f282 to your computer and use it in GitHub Desktop.
Save warpling/829e93a1357e9ef7f282 to your computer and use it in GitHub Desktop.
DictionaryProperties
//
// 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
//
// 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