Objective-C 垃圾收集打开时 xml 元素插入错误
如果未在项目属性选项中打开垃圾收集,以下代码段可以正常工作。但打开 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");
}
The following piece of code worked fine if garbage collection was not turned on in the project properties option. But with GC turned on, this is the error message:
"* Assertion failure in -[NSXMLFidelityElement insertChild:atIndex:], /SourceCache/Foundation/Foundation-751.53/XML.subproj/XMLTypes.subproj/NSXMLElement.m:823"
"Cannot add a child that has a parent; detach or copy first"
Any suggestion?
-(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");
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您会看到,因为 GC 打开,父节点被视为“仍在使用”,因此仍然附加到其子节点。您必须先复制子节点,然后再将其添加到
thisName
:You're seeing that because with GC on, the parent node is seen as "still in use" and so remains attached to its child node. You must copy the child node before adding it to
thisName
:它对我有用感谢 Jonathan Grynspan
Att。卡洛斯·拉米雷斯
it Works for me thanks Jonathan Grynspan
Att. Carlos Ramirez