我想在iOS 3.x中使用一些iOS 4.x的东西。我知道如何实现它们,我认为我的实现速度较慢,或者可能比Apple有更多错误,所以我想在任何时候使用Apple。
正在做这样的事情:
@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
如果这是一个坏主意,那会更好一点?
(如果它是理智的,但代码有点偏离,我也想知道...但我更有兴趣知道整个想法是否合适,或者如果这整个构造应该(避免)