我知道在Objective-C Runtime中定义了protocol_copyMethodDescriptionList
,但我不想太深入,或者使用c数组。 Protocol
对象有任何方法可以做到这一点吗?我在哪里可以找到Protocol
对象的任何文档?我希望能有这样的东西:如何获取协议定义的函数列表?
[foo getMethodsThisProtocolDefines]
;
其中foo是Protocol
。
我知道在Objective-C Runtime中定义了protocol_copyMethodDescriptionList
,但我不想太深入,或者使用c数组。 Protocol
对象有任何方法可以做到这一点吗?我在哪里可以找到Protocol
对象的任何文档?我希望能有这样的东西:如何获取协议定义的函数列表?
[foo getMethodsThisProtocolDefines]
;
其中foo是Protocol
。
您可能需要this.用于Objective-C运行时的Objective-C包装。
Protocol
类自Leopard/ObjC 2.0以来已弃用*。因此,它没有方法,也没有任何当前文档。与协议交互的唯一方法是通过运行时功能。
协议方法列表中包含的结构也不是对象,所以它们无法进入NSArray
而不被封装。
处理从protocol_copyMethodDescriptionList()
返回的数组并不是特别艰难;你只需要记住free()
吧。如果你有一个特定的选择器,你也可以使用protocol_getMethodDescription()
来检查协议,你不需要任何内存管理。例如:
BOOL method_description_isNULL(struct objc_method_description desc)
{
return (desc.types == NULL) && (desc.name == NULL);
}
const char * procure_encoding_string_for_selector_from_protocol(SEL sel, Protocol * protocol)
{
static BOOL isReqVals[4] = {NO, NO, YES, YES};
static BOOL isInstanceVals[4] = {NO, YES, NO, YES};
struct objc_method_description desc = {NULL, NULL};
for(int i = 0; i < 4; i++){
desc = protocol_getMethodDescription(protocol,
sel,
isReqVals[i],
isInstanceVals[i]);
if(!method_description_isNULL(desc)){
break;
}
}
return desc.types;
}
*事实上,它似乎(基于运行时参考a note),这个名字现在只是Class
的别名。
从Objective-C运行时检查protocol_copyMethodDescriptionList。这将返回协议上的一组方法。