先贴代码
typedef IMP *IMPPointer;BOOL class_swizzleMethodAndStore(Class class, SEL original, IMP replacement, IMPPointer store) {
    IMP imp = NULL;
    Method method = class_getInstanceMethod(class, original);
    if (method) {
        const char *type = method_getTypeEncoding(method);
        imp = class_replaceMethod(class, original, replacement, type);
        if (!imp) {
            imp = method_getImplementation(method);
        }
    }
    if (imp && store) { *store = imp; }
    return (imp != NULL);
}@implementation NSObject (FRRuntimeAdditions)
+ (BOOL)swizzle:(SEL)original with:(IMP)replacement store:(IMPPointer)store {
    return class_swizzleMethodAndStore(self, original, replacement, store);
}
@end最近在学习 OC 的 runtime,在 Stack Overflow 上看到了一套完美的 Method Swizzling 代码,其中有几个点不是很懂:
1. class_swizzleMethodAndStore() 函数的最后一个参数 store 是干啥用的
2.这段代码的作者解释,这么写可以替代在 +load 方法中进行 Method Swizzling,因为它是线程安全的,为什么这么写就线程安全了(也可能是我理解错了,请指正)附上链接:http://stackoverflow.com/questions/5339276/what-are-the-dangers-of-method-swizzling-in-objective-c
求指点,谢谢!

解决方案 »

  1.   

    从那个链接里可以看到:
    @implementation NSView (MyViewAdditions)static void MySetFrame(id self, SEL _cmd, NSRect frame);
    static void (*SetFrameIMP)(id self, SEL _cmd, NSRect frame);static void MySetFrame(id self, SEL _cmd, NSRect frame) {
        // do custom work
        SetFrameIMP(self, _cmd, frame);
    }+ (void)load {
        [self swizzle:@selector(setFrame:) with:(IMP)MySetFrame store:(IMP *)&SetFrameIMP];
    }store传入的是SetFrameIMP这个变量的地址,然后通过*store = imp;就可以把imp赋值给SetFrameIMP,之后就可以通过SetFrameIMP(self, _cmd, frame);来调用之前selector映射的方法了。