我想在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