-
-
Save jonsterling/420274 to your computer and use it in GitHub Desktop.
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
// Author: Pierre Bernard, Jonathan Sterling | |
// Source: http://www.bernard-web.com/pierre/blog/index.php?id=2624434753771423706 | |
// Caveat: Consider using http://github.com/andrep/RMModelObject instead. | |
@implementation NSObject (PropertyDealloc) | |
- (void)releaseProperties { | |
Class class = [self class]; | |
unsigned int pCount; | |
objc_property_t *properties = class_copyPropertyList(class, &pCount); | |
for (unsigned int i = 0; i < pCount; i++) { | |
objc_property_t property = properties[i]; | |
NSString *propertyAttributes = [[[NSString alloc] initWithUTF8String:property_getAttributes(property)] autorelease]; | |
NSArray *propertyAttributeArray = [propertyAttributes componentsSeparatedByString:@","]; | |
BOOL isRetained = NO; | |
for (NSString *string in propertyAttributeArray) { | |
isRetained = isRetained || [string isEqual:@"&"] || [string isEqual:@"C"]; | |
} | |
if (isRetained) { | |
NSString *variableName = nil; | |
NSString *lastProperty = (NSString*)[propertyAttributeArray lastObject]; | |
if ([lastProperty hasPrefix:@"V"]) { | |
variableName = [lastProperty substringFromIndex:1]; | |
} | |
if (variableName != nil) { | |
Ivar ivar = class_getInstanceVariable(class, [variableName UTF8String]); | |
id value = object_getIvar(self, ivar); | |
object_setIvar(self, ivar, nil); | |
[value release]; | |
} | |
} | |
} | |
free(properties); | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment