Objective-C 垃圾收集打开时 xml 元素插入错误

发布于 2024-11-08 02:26:55 字数 898 浏览 0 评论 0原文

如果未在项目属性选项中打开垃圾收集,以下代码段可以正常工作。但打开 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

清音悠歌 2024-11-15 02:26:55

您会看到,因为 GC 打开,父节点被视为“仍在使用”,因此仍然附加到其子节点。您必须先复制子节点,然后再将其添加到 thisName

[thisName addChild:[nodeToAdd copy]];

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:

[thisName addChild:[nodeToAdd copy]];
你是年少的欢喜 2024-11-15 02:26:55

它对我有用感谢 Jonathan Grynspan

 NSDecimalNumber *time = message.time;
    NSXMLElement *timex = [[NSXMLElement alloc] initWithName:@"timex"];
    [timex setStringValue:time.stringValue];
    [timex addChild:[timex copy]];
    [timex addChild:timex];

Att。卡洛斯·拉米雷斯

it Works for me thanks Jonathan Grynspan

 NSDecimalNumber *time = message.time;
    NSXMLElement *timex = [[NSXMLElement alloc] initWithName:@"timex"];
    [timex setStringValue:time.stringValue];
    [timex addChild:[timex copy]];
    [timex addChild:timex];

Att. Carlos Ramirez

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文