Advertisement
priore

Remove observe notification and release all properties

Sep 15th, 2012
864
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //  Created by Danilo Priore on 09/16/12.
  2. //  Copyright (c) 2012 Prioregroup.com. All rights reserved.
  3. //
  4. // Automatic remove observe notification, release object and set it nil for all properties.
  5. //
  6. - (void)deallocProperties
  7. {
  8.     Class class = [self class];
  9.     unsigned int pCount;
  10.     objc_property_t *properties = class_copyPropertyList(class, &pCount);
  11.    
  12.     for (unsigned int i = 0; i < pCount; i++) {
  13.         objc_property_t property = properties[i];
  14.         NSString *propertyAttributes = [[[NSString alloc] initWithUTF8String:property_getAttributes(property)] autorelease];
  15.         NSArray *propertyAttributeArray = [propertyAttributes componentsSeparatedByString:@","];
  16.         BOOL isRetained = NO;
  17.        
  18.         for (NSString *string in propertyAttributeArray) {
  19.             isRetained = isRetained || [string isEqual:@"&"] || [string isEqual:@"C"];
  20.             if (isRetained) break;
  21.         }
  22.        
  23.         NSString *variableName = [NSString stringWithCString:property_getName(property) encoding:NSUTF8StringEncoding];
  24.         if (variableName != nil) {
  25.             [self removeObserver:self forKeyPath:variableName];
  26.            
  27.             if (isRetained) {
  28.                 Ivar ivar = class_getInstanceVariable(class, [variableName UTF8String]);
  29.                 id value = object_getIvar(self, ivar);
  30.                
  31.                 if (value != nil) {
  32.                     [value release];
  33.                     object_setIvar(self, ivar, nil);
  34.                 }
  35.             }
  36.         }
  37.     }
  38.    
  39.     free(properties);
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement