Finding classes that declare that they conform to a specific protocol using the Objective-C runtime

Viewed 2821

I'm trying to figure out a way to "tag" classes which will be written later so I can find them at runtime, but without enforcing the usage of a specific parent classes. Currently I'm looking at perhaps applying a protocol and then finding all classes which have that protocol.

But I've not be able to figure out how.

Does anyone know if it's possible to find all classes which implement a specific protocol at runtime? or alternatively - is there a better way to "tag" classes and find them?

3 Answers

Just in case anyone is interested in this old question, I eventually wrote some code the located all the bundles for the application and scanned the classes in those bundles. This was far more efficient than using code based on objc_getClassList because it scanned a limited number of classes as opposed to the entire runtime.

If you are interested you can find the code I was using here: https://github.com/drekka/Alchemic/blob/master/alchemic/NSBundle%2BAlchemic.m

Related