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);
}
  • 6
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值