Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save jonsterling/420274 to your computer and use it in GitHub Desktop.
Save jonsterling/420274 to your computer and use it in GitHub Desktop.
// 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