类型“struct objc_method”的不完整定义
我真的很困惑这个问题。我需要做的是在我的项目中使用一些 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;
}
这是否类似于前向声明问题,还是其他问题?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
除了 Martin 的回答之外,您还应该使用诸如
method_getName
之类的函数来检索方法classMethods[i]
的名称。这更加可移植(特别是从 Objective-C 2.0 开始,结构中的这些字段就不再存在,正如宏所暗示的那样),并且可以避免像运行时发展时遇到的问题。
In addition to Martin answer, you should use functions like
method_getName
to retrieve the name of the methodclassMethods[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.
这些成员无法访问!
OBJC2_UNAVAILABLE 宏指示成员不可用,并且仅提供对结构的一些了解。
These members cannot be accessed!
OBJC2_UNAVAILABLE macro indicates that a member is not available and only provides some insight into the structure.