我想创建一个类,我想使用类的属性名称和属性值将其序列化为 XML。为此,我在我的基类中创建了以下函数(我将从中派生所有其他类):
- (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
我假设所有属性都是(非原子的,强的)NSString*(现在 - 稍后会出现更复杂的代码)。现在由于某种原因,当我上线时
[s appendFormat: @"<%@>%@</%@>", name, (NSString*)property], name];
我收到 EXC_BAD_ACCESS 异常。我究竟做错了什么?提前致谢!