Created
February 25, 2011 19:34
-
-
Save cyborch/844360 to your computer and use it in GitHub Desktop.
Why something like this isn't done by NSObject#dealloc is beyond me...
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 NSObject (AutoreleaseProperties) | |
- (void)releaseAllProperties { | |
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; | |
unsigned int outCount, i; | |
objc_property_t *properties = class_copyPropertyList([self class], &outCount); | |
for (i = 0; i < outCount; i++) { | |
objc_property_t property = properties[i]; | |
NSArray *propertyAttributes = [[NSString stringWithCString: property_getAttributes(property) | |
encoding: NSASCIIStringEncoding] componentsSeparatedByString: @","]; | |
// If this property needs to be released | |
if ([propertyAttributes containsObject: @"R"] || [propertyAttributes containsObject: @"C"]) { | |
// Find default setter name | |
NSString *name = [NSString stringWithCString: property_getName(property) | |
encoding: NSASCIIStringEncoding]; | |
NSString *setter = [NSString stringWithFormat: @"set%@%@:", | |
[[name substringToIndex: 1] uppercaseString], | |
[name substringFromIndex: 1]]; | |
// Find custom setter name | |
for (NSString *attribute in propertyAttributes) { | |
if ([attribute hasPrefix: @"S"]) setter = [attribute substringFromIndex: 1]; | |
} | |
// Set the property to nil | |
[self performSelector: NSSelectorFromString(setter) | |
withObject: nil]; | |
} | |
} | |
free(properties); | |
[pool release]; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment