Created
July 17, 2012 11:50
-
-
Save priore/3128993 to your computer and use it in GitHub Desktop.
Copy object with property values
This file contains 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
// 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