Search code examples
iosobjective-cobjective-c-categoryswizzling

Swizzle a class without an instance of it


I need to swizzle a class that I don't have a reference to at the point I'm trying to swizzle at. I'm using the "indie" swizzling method so that I can swizzle any class from anywhere, but it requires an instance of that class to get the Class reference.

So the example code has this line:

Class class = [NSURLSession sharedSession].class;

However, I need to do something like:

Class class = classof(ViewController); //ViewController is a class, not an instance

Or something like that. Is this possible in ObjectiveC?


Solution

  • You do it the exact same way :

    Class class = ViewController.class;