Skip to content

Instantly share code, notes, and snippets.

@priore
Created July 5, 2016 12:41
Show Gist options
  • Select an option

  • Save priore/f760a3841be623029697848617c193b9 to your computer and use it in GitHub Desktop.

Select an option

Save priore/f760a3841be623029697848617c193b9 to your computer and use it in GitHub Desktop.
Tips: a solid dealloc for all classes
#import <objc/runtime.h>
@implementation BaseViewController()
- (void)dealloc
{
[CATransaction setCompletionBlock:nil];
[NSObject cancelPreviousPerformRequestsWithTarget:self];
[[NSNotificationCenter defaultCenter] removeObserver:self];
if ([self respondsToSelector:@selector(setDelegate:)])
[self performSelector:@selector(setDelegate:) withObject:nil];
// invalidate timers
unsigned int count = 0;
objc_property_t *properties = class_copyPropertyList([self class], &count);
for (int i = 0; i < count; ++i) {
NSString *name = [NSString stringWithCString:property_getName(properties[i]) encoding:NSUTF8StringEncoding];
id prop = [self valueForKey:name];
if ([prop respondsToSelector:@selector(invalidate)])
[prop invalidate], prop = nil;
}
[super dealloc]; // not needed in ARC
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment