我尝试使用swizzling方法来捕获NSObject泛型的“”。
NSObject *obj = [[NSObject alloc] init];
UIView *view = [[UIView alloc] initWithFrame:CGRectZero];
NSString *str = [[NSString alloc] init];
[...]这是在所有其他方法上工作的实现(类别.m),除了NSObjet上的alloc之外,还有。
可能是什么原因?
#import "NSObject+Custom.h"
#import <objc/runtime.h>
@implementation NSObject (Custom)
+ (void)load
{
Method original = class_getInstanceMethod(self, @selector(alloc));
Method swizzle = class_getInstanceMethod(self, @selector(allocCustom));
method_exchangeImplementations(original, swizzle);
}
- (id)allocCustom
{
NSLog(@"%s", __FUNCTION__); // no way
return [self allocCustom];
}
@end谢谢。
发布于 2013-05-29 10:48:40
+alloc是类方法,而不是实例方法:
class_getClassMethod而不是class_getInstanceMethod+allocCustom而不是-allocCustomhttps://stackoverflow.com/questions/16811461
复制相似问题