【发布时间】:2012-07-22 17:54:54
【问题描述】:
我知道在 Objective-C 运行时中定义了 protocol_copyMethodDescriptionList,但我不想深入研究,也不想使用 c 数组。 Protocol 对象是否有任何方法可以做到这一点?我在哪里可以找到Protocol 对象的任何文档?我希望是这样的:
[foo getMethodsThisProtocolDefines];
其中 foo 是 Protocol。
【问题讨论】:
我知道在 Objective-C 运行时中定义了 protocol_copyMethodDescriptionList,但我不想深入研究,也不想使用 c 数组。 Protocol 对象是否有任何方法可以做到这一点?我在哪里可以找到Protocol 对象的任何文档?我希望是这样的:
[foo getMethodsThisProtocolDefines];
其中 foo 是 Protocol。
【问题讨论】:
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 的别名。
【讨论】:
您可能需要 this. Objective-C 运行时的 Objective-C 包装器。
【讨论】:
从 Objective-C 运行时检查 protocol_copyMethodDescriptionList。这将返回协议上的一组方法。
【讨论】: