ios runtime 動態向類添加方法 – iPhone手機開發技術文章 iPhone軟體開發教學課程

1、定義C函數:

void dynamicMethodIMP(id
self, SEL _cmd)

{

NSLog(@”蝸牛也瘋狂”);

}

2、重寫函數+(BOOL)resolveInstanceMethod:(SEL)sel

+(BOOL)resolveInstanceMethod:(SEL)sel

{

class_addMethod([self
class], sel, (IMP)dynamicMethodIMP,
“v@:”);

return [super
resolveInstanceMethod:sel];

}

3、使用:

SEL select =
NSSelectorFromString(@”resolveThisMethodDynamically”);

[class
resolveInstanceMethod:select];

[[class instance] performSelector:select];

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *