2

我知道protocol_copyMethodDescriptionList在 Objective-C 运行时中有定义,但我不想走得那么深,或者使用 c 数组。有没有Protocol可以做到这一点的对象的方法?我在哪里可以找到该Protocol对象的任何文档?我希望有类似的东西:

[foo getMethodsThisProtocolDefines];

其中 foo 是一个Protocol

4

3 回答 3

5

自 Leopard/ObjC 2.0 以来,Protocol该类已被弃用。* 因此,它没有任何方法,也没有任何当前文档。与协议交互的唯一方法是通过运行时函数。

协议的方法列表中包含的结构也不是对象,因此无论如何它们都无法进入 anNSArray而不被包装。

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;
}

*事实上,似乎(基于运行时参考中的注释)该名称现在只是Class.

于 2012-07-22T18:38:24.067 回答
1

你可能想要这个。Objective-C 运行时的 Objective-C 包装器。

于 2012-07-22T18:04:51.770 回答
1

从 Objective-C 运行时检查protocol_copyMethodDescriptionList 。这将返回协议上的一组方法。

于 2017-05-21T15:54:31.253 回答