如何查看 objective C 协议的内容?

How can I view the contents of an objective C Protocol?

如果我可以访问 objective-C 协议并试图弄清楚如何查看它的内部以查看它包含哪些方法,包括它们的签名等

我已经尝试过 NSLog 并在调试器中以及在 Internet 上查看对象,但找不到任何方法来执行此操作。

在看到这个 SO post 的答案后,我检查了 objc/runtime.h 中的方法:List selectors for Objective-C object 并找到了 NSLog 协议方法签名的方法

#import <objc/runtime.h>

Protocol *protocol = @protocol(UITableViewDelegate);

BOOL showRequiredMethods = NO;
BOOL showInstanceMethods = YES;

unsigned int methodCount = 0;
struct objc_method_description *methods = protocol_copyMethodDescriptionList(protocol, showrequiredMethods, showInstanceMethods, &methodCount);

NSLog(@"%d required instance methods found:", methodCount);

for (int i = 0; i < methodCount; i++)
{
    struct objc_method_description methodDescription = methods[i];
    NSLog(@"Method #%d: %@", i, NSStringFromSelector(methodDescription.name));
}

free(methods)

唯一的问题是,在 protocol_copyMethodDescriptionList 中,您需要指定是需要必需方法还是不需要方法,以及是否需要 class 与实例方法。因此,要涵盖所有四种情况,您需要调用 protocol_copyMethodDescriptionList 四次并打印每个列表的结果。