我想使用该类的属性名称和属性值创建一个要序列化为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异常。
我究竟做错了什么?
提前致谢!
最佳答案
属性以c字符串而不是NSString的形式返回。只需更改格式以打印出c字符串,而不是尝试转换它,就像这样:
[s appendFormat: @"<%@>%s</%@>", name, property, name];
关于ios - 访问属性的EXC_BAD_ACCESS异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14734952/