Advertisement
priore

Tips: a solid dealloc for all classes

Jun 23rd, 2014
523
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #import <objc/runtime.h>
  2.  
  3. - (void)dealloc
  4. {
  5.     [CATransaction setCompletionBlock:nil];
  6.     [NSObject cancelPreviousPerformRequestsWithTarget:self];
  7.     [[NSNotificationCenter defaultCenter] removeObserver:self];
  8.    
  9.     if ([self respondsToSelector:@selector(setDelegate:)])
  10.             [self performSelector:@selector(setDelegate:) withObject:nil];
  11.    
  12.     // invalidate timers
  13.     unsigned int count = 0;
  14.     objc_property_t *properties = class_copyPropertyList([self class], &count);
  15.     for (int i = 0; i < count; ++i) {
  16.         NSString *name = [NSString stringWithCString:property_getName(properties[i]) encoding:NSUTF8StringEncoding];
  17.         id prop = [self valueForKey:name];
  18.         if ([prop respondsToSelector:@selector(invalidate)])
  19.             [prop invalidate], prop = nil;
  20.     }
  21.  
  22.     [super dealloc]; // not needed in ARC
  23. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement