Created
July 5, 2016 12:41
-
-
Save priore/f760a3841be623029697848617c193b9 to your computer and use it in GitHub Desktop.
Tips: a solid dealloc for all classes
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #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