Skip to content

Instantly share code, notes, and snippets.

@priore
Created July 17, 2012 11:50
Show Gist options
  • Save priore/3128993 to your computer and use it in GitHub Desktop.
Save priore/3128993 to your computer and use it in GitHub Desktop.
Copy object with property values
// Created by Danilo Priore on 02/04/12.
// Copyright (c) 2011 Prioregroup.com. All rights reserved.
//
// Copy object with property values
//
- (id)copy {
id copied = [[[self class] alloc] init];
unsigned int count = 0;
objc_property_t *properties = class_copyPropertyList([self class], &count);
for (int i = 0; i < count; ++i) {
objc_property_t property = properties[i];
NSString *propertyAttributes = [[[NSString alloc] initWithUTF8String:property_getAttributes(property)] autorelease];
NSArray *propertyAttributeArray = [propertyAttributes componentsSeparatedByString:@","];
BOOL isReadOnly = NO;
for (NSString *string in propertyAttributeArray) {
isReadOnly = isReadOnly || [string isEqual:@"R"];
}
if (!isReadOnly) {
NSString *name = [NSString stringWithCString:property_getName(properties[i]) encoding:NSUTF8StringEncoding];
[copied setValue:[self valueForKey:name] forKey:name];
}
}
free(properties);
return [copied autorelease];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment