tags:

views:

27

answers:

0

I want to use some of the iOS 4.x things in iOS 3.x. I know how to implement them, I assume my implementation is slower or might have more bugs then Apple's so I want to use Apple's any time it is available.

Is doing something like this sane:

@implementation NSDictionary (NSDictionary_4)

void compatability_method(Class class, SEL newer_version_selector, SEL compatability_selector) {
    Method existingMethod = class_getInstanceMethod(class, newer_version_selector);
    if (!existingMethod) {
        Method compatabilityMethod = class_getInstanceMethod(class, compatability_selector);
        class_replaceMethod(class, newer_version_selector, method_getImplementation(compatabilityMethod), method_getTypeEncoding(compatabilityMethod));
    }
}

+(void)load {
    if (self == [NSDictionary class]) {
        compatability_method(self, @selector(enumerateKeysAndObjectsUsingBlock:), @selector(four_enumerateKeysAndObjectsUsingBlock:));
    }
}

-(void)four_enumerateKeysAndObjectsUsingBlock:(void (^)(id key, id obj, BOOL *stop))block {
    BOOL stop = false;

    for (id key in self) {
        block(key, [self objectForKey:key], &stop);
        if (stop) {
            break;
        }
    }
}


@end

If it is a bad idea, what would be better?

(if it is sane, but the code is a bit off, I would like to know that too...but I'm more interested to know if the entire idea is decent, or if this entire sort of construct should be avoided)