How to swizzle a method of a private class

拟墨画扇 提交于 2019-12-18 15:39:13

问题


I have a private class (both declared & defined within .m) as an addition to an implementation of a different class, that happens to use that private class internally.

I'd like to swizzle one of the methods of that private class.

I defined a category and did the usual:

+(void)load 
{
    Method original, swizzled;

    original = class_getInstanceMethod(objc_getClass("SomePrivateClass"), @selector(somePrivateMethod:));
    swizzled = class_getInstanceMethod(self, @selector(swizzled_somePrivateMethod:));
    method_exchangeImplementations(original, swizzled); 
}

The issue is that my implementation obviously doesn't know anything about this private class and self refers to the class I am adding the category to, whichever class that might be. So I have no way of calling the original implementation and in general working with the private class.

What is the proper approach to tackle this?


回答1:


Managed to get this to work, it's pretty simple actually.

So the way I did it:

  • made a NSObject category: @interface NSObject(PrivateSwizzleCategory)
  • swizzled:

    +(void)load
    {
        Method original, swizzled;
    
        original = class_getInstanceMethod(objc_getClass("SomePrivateClass"), @selector(somePrivateMethod:));
        swizzled = class_getInstanceMethod(self, @selector(swizzled_somePrivateMethod:));   
        method_exchangeImplementations(original, swizzled);
    }
    
  • To call the original implementation, I had to cast self to NSObject:

    id ret = [(NSObject *)self swizzled_somePrivateMethod:someParam];
    
  • To access private properties of the private class, I used valueForKey on self:

    id privateProperty = [self valueForKey:@"__privateProperty"];
    

Everything works!



来源:https://stackoverflow.com/questions/22361427/how-to-swizzle-a-method-of-a-private-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!