jeudi 7 décembre 2017

Invoking an objective-c private class's method in Swift using reflection

At first, I want to invoke a private class's instance method from a objective-c static framework.

If I use OC, I can invoke it like this:

Class TargetClass = NSClassFromString(@"TheNameOfTargetClass");
SEL selector = NSSelectorFromString(@"sharedInstance");
IMP imp = [FLEXNetworkRecorderClass methodForSelector:selector];
id (*func)(id, SEL) = (__typeof__(func))imp;
id instance = func(TargetClass,selector);
if(instance){
    SEL selector = NSSelectorFromString(@"targetFunction");
    IMP imp = [defaultRecorder methodForSelector:selector];
    id (*func)(id, SEL) = (__typeof__(func))imp;
    id result = func(instance,selector);
}

The result is what I want, but How to do it in Swift?

I have tried a lot, but I can I don't know how to translate some of above, like:

id (*func)(id, SEL) = (__typeof__(func))imp;

and if I want to invoke methodForSelector I need to convert to NSObject using:

as! NSObject

and then, it crash.

So how to that?

Any advice will be appreciated.





Aucun commentaire:

Enregistrer un commentaire