使用 Objective-C 运行时查找声明它们符合特定协议的类 [英] Finding classes that declare that they conform to a specific protocol using the Objective-C runtime

查看:59
本文介绍了使用 Objective-C 运行时查找声明它们符合特定协议的类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试找出一种标记"类的方法,这些类将在稍后编写,以便我可以在运行时找到它们,但不强制使用特定的父类.目前我正在考虑应用一个协议,然后找到所有具有该协议的类.

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?

推荐答案

为了防止有人对这个老问题感兴趣,我最终编写了一些代码来定位应用程序的所有包并扫描这些包中的类.这比使用基于 objc_getClassList 的代码高效得多,因为它扫描了有限数量的类,而不是整个运行时.

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.

如果您有兴趣,可以在这里找到我使用的代码:https://github.com/drekka/Alchemic/blob/master/alchemic/NSBundle%2BAlchemic.m

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

这篇关于使用 Objective-C 运行时查找声明它们符合特定协议的类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆