Advertisement
priore

Copy object with property values

Apr 1st, 2012
964
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //  Created by Danilo Priore on 02/04/12.
  2. //  Copyright (c) 2011 Prioregroup.com. All rights reserved.
  3. //
  4. //  Copy object with property values
  5. //
  6. - (id)copy {
  7.    
  8.     id copied = [[[self class] alloc] init];
  9.     unsigned int count = 0;
  10.     objc_property_t *properties = class_copyPropertyList([self class], &count);
  11.     for (int i = 0; i < count; ++i) {
  12.         objc_property_t property = properties[i];
  13.         NSString *propertyAttributes = [[[NSString alloc] initWithUTF8String:property_getAttributes(property)] autorelease];
  14.         NSArray *propertyAttributeArray = [propertyAttributes componentsSeparatedByString:@","];
  15.         BOOL isReadOnly = NO;
  16.        
  17.         for (NSString *string in propertyAttributeArray) {
  18.             isReadOnly = isReadOnly || [string isEqual:@"R"];
  19.         }
  20.  
  21.         if (!isReadOnly) {
  22.             NSString *name = [NSString stringWithCString:property_getName(properties[i]) encoding:NSUTF8StringEncoding];
  23.                 [copied setValue:[self valueForKey:name] forKey:name];
  24.         }
  25.     }
  26.     free(properties);
  27.    
  28.     return [copied autorelease];
  29. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement