我想在Obj-c runtime reference添加方法动态
BOOL class_addMethod(Class cls, SEL name, IMP imp, const char *types)
我想补充像一个新的方法,发现这个方法:
- [AClass drawWithFrame:(NSRect)rect inView:(id)view]
到目前为止,我已经写了一个C函数:
void drawWithFrameInView(id this, SEL this_cmd, NSRect frame, id view){
...
}
现在我准备好了:
BOOL success = class_addMethod(NSClassFromString(@"AClass"),
@selector(drawWithFrame:inView:),
(IMP)drawWithFrameInView,
"[email protected]:@:@:");
但success
从来没有是,我已经尝试了与更简单的签名方法相同的方法,它的工作。所以我认为问题是最后一个参数:"[email protected]:@:@:"
在这种情况下我应该通过什么来让我的新方法工作?
为什么不命名前两个参数'self'和'_cmd',这样你就可以写出你在方法中写入的相同代码? –
嗯......只是出于美学原因。从C的角度来看'self'和'_cmd'应该是正常的参数。我不喜欢在Obj-C中将参数着色为关键字的事实(即使它们代表这些关键字)。这是所有:) – nacho4d