Javascript style objects in Objective-C

Posted by awolf on Stack Overflow See other posts from Stack Overflow or by awolf
Published on 2010-05-25T21:37:51Z Indexed on 2010/05/25 21:41 UTC
Read the original article Hit count: 186

Background: I use a ton of NSDictionary objects in my iPhone and iPad code. I'm sick of the verbose way of getting/setting keys to these state dictionaries.

So a little bit of an experiment: I just created a class I call Remap.

Remap will take any arbitrary set[VariableName]:(NSObject *) obj selector and forward that message to a function that will insert obj into an internal NSMutableDictionary under the key [vairableName].

Remap will also take any (zero argument) arbitrary [variableName] selector and return the NSObject mapped in the NSMutableDictionary under the key [variableName].

e.g.

Remap * remap = [[Remap alloc] init];

NSNumber * testNumber = [NSNumber numberWithInt:46];
[remap setTestNumber:testNumber];
testNumber = [remap testNumber];

[remap setTestString:@"test string"];
NSString * testString = [remap testString];

NSMutableDictionary * testDict = [NSMutableDictionary dictionaryWithObject:testNumber forKey:@"testNumber"];
[remap setTestDict:testDict];
testDict = [remap testDict];

where none of the properties testNumber, testString, or testDict are actually defined in Remap.

The crazy thing? It works... My only question is how can I disable the "may not respond to " warnings for JUST accesses to Remap?

P.S. : I'll probably end up scrapping this and going with macros since message forwarding is quite inefficient... but aside from that does anyone see other problems with Remap?

Here's Remap's .m for those who are curious:

#import "Remap.h"

@interface Remap ()
@property (nonatomic, retain)   NSMutableDictionary * _data;
@end

@implementation Remap

@synthesize _data;

- (void) dealloc
{
    relnil(_data);
    [super dealloc];
}

- (id) init
{
    self = [super init];
    if (self != nil) {
        NSMutableDictionary * dict = [[NSMutableDictionary alloc] init];
        [self set_data:dict];
        relnil(dict);
    }
    return self;
}


- (void)forwardInvocation:(NSInvocation *)anInvocation
{
    NSString * selectorName = [NSString stringWithUTF8String: sel_getName([anInvocation selector])];

    NSRange range = [selectorName rangeOfString:@"set"];

    NSInteger numArguments = [[anInvocation methodSignature] numberOfArguments];

    if (range.location == 0 && numArguments == 4)
    {
        //setter
        [anInvocation setSelector:@selector(setData:withKey:)];
        [anInvocation setArgument:&selectorName atIndex:3];
        [anInvocation invokeWithTarget:self];
    }
    else if (numArguments == 3)
    {
        [anInvocation setSelector:@selector(getDataWithKey:)];
        [anInvocation setArgument:&selectorName atIndex:2];
        [anInvocation invokeWithTarget:self];
    }


}

- (NSMethodSignature *) methodSignatureForSelector:(SEL) aSelector
{
    NSString * selectorName = [NSString stringWithUTF8String: sel_getName(aSelector)];

    NSMethodSignature * sig = [super methodSignatureForSelector:aSelector];

    if (sig == nil)
    {
        NSRange range = [selectorName rangeOfString:@"set"];

        if (range.location == 0)
        {
            sig = [self methodSignatureForSelector:@selector(setData:withKey:)];
        }
        else
        {
            sig = [self methodSignatureForSelector:@selector(getDataWithKey:)];
        }
    }

    return sig;
}

- (NSObject *) getDataWithKey: (NSString *) key
{
    NSObject * returnValue = [[self _data] objectForKey:key];
    return returnValue;
}


- (void) setData: (NSObject *) data withKey:(NSString *)key
{
    if (key && [key length] >= 5 && data)
    {
        NSRange range;
        range.length = 1;
        range.location = 3;

        NSString * firstChar = [key substringWithRange:range];
        firstChar = [firstChar lowercaseString];

        range.length = [key length] - 5; // the 4 we have processed plus the training :
        range.location = 4;

        NSString * adjustedKey = [NSString stringWithFormat:@"%@%@", firstChar, [key substringWithRange:range]];

        [[self _data] setObject:data forKey:adjustedKey];
    }
    else 
    {
        //assert?
    }
}

@end

© Stack Overflow or respective owner

Related posts about iphone

Related posts about objective-c