iOS 实例方法混淆

iOS 实例方法混淆

奇技yin巧 指过于奇巧而无益的技艺与制品.

转载请注明出处http://blog.csdn.net/uxyheaven/article/details/44265539
系列文章请看http://blog.csdn.net/uxyheaven/article/category/5800569

IMS指的是 Instance Method Swizzling, 实例方法混淆.
下段代码是一个Instance Method Swizzling和一个Method Swizzling的例子:

// Man.m
- (void)run
{
    NSLog(@"%s, %@", __func__, _name);
}
- (void)jump
{
    NSLog(@"%s, %@", __func__, _name);
}
- (void)handsUp
{
    NSLog(@"%s, %@", __func__, _name);
}
- (void)handsDown
{
    NSLog(@"%s, %@", __func__, _name);
}

// ViewController.m
- (void)viewDidLoad {
    ...
    Man *a = [Man manWithName:@"a"];
    Man *b = [Man manWithName:@"b"];

    [self swizzleInstanceMethodWithInstance:a originalSel:@selector(run) replacementSel:@selector(jump)];
    [self swizzleInstanceMethodWithClass:[Man class] originalSel:@selector(handsUp) replacementSel:@selector(handsDown)];

    [a run];
    [b run];

    [a handsUp];
    [b handsUp];
}


// 输出的结果是
2015-03-14 23:53:39.832 testRuntime[2196:629365] -[Man jump], a
2015-03-14 23:53:39.833 testRuntime[2196:629365] -[Man run], b
2015-03-14 23:53:39.833 testRuntime[2196:629365] -[Man handsDown], a
2015-03-14 23:53:39.833 testRuntime[2196:629365] -[Man handsDown], b

为什么run方法是只有对象a被替换了,handsUp方法是都被替换了呢?
我们先来看下普通的Method Swizzling是如何实现的

- (void)swizzleInstanceMethodWithClass:(Class)clazz originalSel:(SEL)original replacementSel:(SEL)replacement { Method a = class_getInstanceMethod(clazz, original); Method b = class_getInstanceMethod(clazz, replacement); // class_addMethod 为该类增加一个新方法 if (class_addMethod(clazz, original, method_getImplementation(b), method_getTypeEncoding(b))) { // 替换类方法的实现指针 class_replaceMethod(clazz, replacement, method_getImplementation(a), method_getTypeEncoding(a)); } else { // 交换2个方法的实现指针 method_exchangeImplementations(a, b); } }

Instance Method Swizzling是用了类似KVO的办法.
先动态添加一个类MySubclass继承自原来的类,然后修改对象a的isa为新类,再替换掉新类的方法.

- (void)swizzleInstanceMethodWithInstance:(id)object originalSel:(SEL)original replacementSel:(SEL)replacement { Class newClass = objc_allocateClassPair([object class], "MySubclass", 0); objc_registerClassPair(newClass); Method a = class_getInstanceMethod(newClass, original); Method b = class_getInstanceMethod([object class], replacement); if (class_addMethod(newClass, original, method_getImplementation(b), method_getTypeEncoding(b))) { class_replaceMethod(newClass, replacement, method_getImplementation(a), method_getTypeEncoding(a)); } else { method_exchangeImplementations(a, b); } object_setClass(object, newClass); }

你可能感兴趣的:(ios,objective,Swizzling)