I want to create a class that I want to serialize as XML using the property names and property values of the class. For that I created the following function in my base class (from which I will derive all my other classes):
- (NSString*) serialize
{
unsigned int outCount, i;
NSMutableString* s = [NSMutableString string];
Class currentClass = [self class];
while(currentClass != nil) {
objc_property_t *properties = class_copyPropertyList([self class], &outCount);
if(outCount > 0) {
for(i = 0; i < outCount; i++) {
objc_property_t property = properties[i];
NSString *name = [NSString stringWithCString: property_getName(property) encoding: NSUTF8StringEncoding];
[s appendFormat: @"<%@>%@</%@>", name, (NSString*)property, name];
}
}
free(properties);
}
return (NSString*)s;
}
@end
I am assuming that all the properties are (nonatomic,strong) NSString* (for now – a more sophisticated code would come later). Now for some reason when I hit the line
[s appendFormat: @"<%@>%@</%@>", name, (NSString*)property], name];
I am getting a EXC_BAD_ACCESS exception.
What am I doing wrong?
Thanks in advance!
Found it. Just use
to get the property value…