我在NSObject上有一个类别,它应该做一些事情。 当我在对象上调用它时,我想重写其dealloc方法以进行一些清理。
我想使用方法交换来实现它,但是我无法弄清楚如何做到这一点。 我发现的唯一示例是如何替换整个类的方法实现(在我的情况下,它将覆盖所有NSObjects的dealloc - 这是我不想要的)。
我想覆盖特定NSObject实例的dealloc方法。
我想使用方法交换来实现它,但是我无法弄清楚如何做到这一点。 我发现的唯一示例是如何替换整个类的方法实现(在我的情况下,它将覆盖所有NSObjects的dealloc - 这是我不想要的)。
我想覆盖特定NSObject实例的dealloc方法。
@interface NSObject(MyCategory)
-(void)test;
@end
@implementation NSObject(MyCategory)
-(void)newDealloc
{
// do some cleanup here
[self dealloc]; // call actual dealloc method
}
-(void)test
{
IMP orig=[self methodForSelector:@selector(dealloc)];
IMP repl=[self methodForSelector:@selector(newDealloc)];
if (...) // 'test' might be called several times, this replacement should happen only on the first call
{
method_exchangeImplementations(..., ...);
}
}
@end
NSObject
,可以让您注册块,在任何对象的dealloc期间调用。 - Sven