如果未在项目属性选项中打开垃圾收集,以下代码可以正常工作。但是当 GC 打开时,这是错误消息:
“* -[NSXMLFidelityElement insertChild:atIndex:]、/SourceCache/Foundation/Foundation-751.53/XML.subproj/XMLTypes.subproj/NSXMLElement.m:823 中断言失败” “无法添加具有父级的子级;先分离或复制”
有什么建议吗?
-(void)insertXmlRecord
{
//xmlDoc is an iVar
NSXMLElement *nodeToAdd = [[NSXMLElement alloc] initWithXMLString:[self readOnScreenSetAttrib] error:nil];
NSError *err=nil;
NSXMLElement *thisName;
NSArray *nodes = [xmlDoc nodesForXPath:@"./dream" error:&err];
NSLog(@"insertXMLRecord xmldoc %@", xmlDoc);
if ([nodes count] > 0 )
{
thisName = [nodes objectAtIndex:0];
NSLog(@"insertXMLRecord: thisname: %@", thisName);
NSLog(@"insertXMLRecord: nodeToAdd: %@", nodeToAdd);
[thisName addChild:nodeToAdd];
}
//NSLog(@"insertXMLRecord");
}
最佳答案
您会看到,因为 GC 开启,父节点被视为“仍在使用”,因此仍然附加到其子节点。您必须先复制子节点,然后再将其添加到 thisName
:
[thisName addChild:[nodeToAdd copy]];
关于objective-c - Objective-C 垃圾收集打开时 xml 元素插入错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6048072/