動的にメソッドを書き換える
#import <objc/runtime.h> id length(id self, SEL _cmd, ...) { return 0; } int main(int argc, char *argv[]) { NSString *str = @"abcdefg"; NSLog(@"%d", [str length]);// => 7 Method method = class_getInstanceMethod([str class], @selector(length)); method->method_imp = length; NSLog(@"%d", [str length]);// => 0 return 0; }
'method_imp' is deprecated (非推奨)なので注意