RSDeallocHandler is a simple NSObject
category for adding and removing block handlers for object's dealloc.
[someObject rs_addDeallocHandler:^{
NSLog(@"SomeObject deallocated.");
} owner:nil];
Handlers may be removed using the ID received on adding.
-(void)someMethod{
_handlerID = [someObject rs_addDeallocHandler:^{} owner:nil];
}
-(void)dealloc{
[someObject rs_removeDeallocHandler:_handlerID];
}
If you specify the owner
parameter then the handler will be automatically removed from the receiver and deallocated when the owner object dies. So you do not need to manually remove the handler in dealloc
.
[someObject rs_addDeallocHandler:^{} owner:self];
Add RSDeallocHandler
to your Podfile.
- iOS 5.0+
- Mac OS X 10.7+
- ARC
- RSSwizzle
Yan Rabovik (@rabovik on twitter)
MIT License.
- 1.1.1 Uses Associated Objects for Toll-Free Bridged classes on iOS 5 because
-dealloc
swizzling can not be used for them. - 1.1.0 Uses
-dealloc
swizzling via RSSwizzle. - 1.0.0 Uses Associated Objects.