【问题标题】:Incomplete definition of type "struct objc_method"类型“struct objc_method”的不完整定义
【发布时间】:2011-11-28 08:49:20
【问题描述】:

我真的对这个问题感到困惑。我需要做的是在我的项目中使用一些 obj-c 运行时特性。这是我的 .m 文件中的简单代码:

#import "Base.h"
#import <objc/runtime.h>

@implementation Base
- (void)someMethod {
    NSUInteger numberMethods = 0;
    Method *classMethods = class_copyMethodList([self class], &numberMethods);
    for (int i = 0; i < numberMethods; i ++) {
        classMethods[i]->method_name; //incomplete definition of type "struct objc_method"
    }
@end

我收到以下错误:“struct objc_method”类型的定义不完整。在检查了一些 objc/runtime.h 文件后,我发现了这样的内容:

一些代码...

typedef struct objc_method *Method;

...一些代码...

struct objc_method {
    SEL method_name                                          OBJC2_UNAVAILABLE;
    char *method_types                                       OBJC2_UNAVAILABLE;
    IMP method_imp                                           OBJC2_UNAVAILABLE;
}

这类似于前向声明问题,还是其他问题?

【问题讨论】:

    标签: objective-c ios runtime forward-declaration


    【解决方案1】:

    除了 Martin 的回答,你应该使用类似 method_getName 的函数来检索方法的名称 classMethods[i]

    这更便于移植(尤其是结构中的这些字段,正如宏所暗示的那样,自 Objective-C 2.0 以来不再存在),并且可以避免运行时演变时遇到的问题。

    【讨论】:

    • 哦,我爱你!:) 非常感谢!
    【解决方案2】:

    这些成员无法访问! OBJC2_UNAVAILABLE 宏指示成员不可用,仅提供对结构的一些了解。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-08-27
      • 1970-01-01
      • 2021-05-16
      • 1970-01-01
      • 2017-08-16
      • 2012-02-16
      • 2021-03-19
      相关资源
      最近更新 更多