【发布时间】:2013-02-06 17:16:41
【问题描述】:
我想创建一个类,我想使用类的属性名称和属性值将其序列化为 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 异常。 我究竟做错了什么? 提前致谢!
【问题讨论】:
-
你的意思是[s appendFormat:@"%@%@>", name, (NSString*)property, name]; not [s appendFormat: @"%@%@>", name, (NSString*)property], name];对吗?
-
@MSK,对!在原始问题中修复了它。
标签: ios objective-c properties