Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Incomplete definition of type "struct objc_method"

I'm really confused with this problem. What I need to do is use some obj-c runtime feature in my project. Here is simple code in my .m file:

#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

I got the following error: incomplete definition of type "struct objc_method". After some inspecting objc/runtime.h file I found something like this:

some code...

typedef struct objc_method *Method;

...some code...

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

Is this something like forward declaration problem, or something else?

like image 265
patrick Avatar asked Feb 23 '23 20:02

patrick


1 Answers

In addition to Martin answer, you should use functions like method_getName to retrieve the name of the method classMethods[i].

This is much more portable (especially these fields in the structure no longer exists since Objective-C 2.0 as the macro suggests) and will avoid problems like the one you have when the Runtime evolves.

like image 162
AliSoftware Avatar answered Feb 25 '23 11:02

AliSoftware