问题
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