The following piece of code worked fine if garbage collection开发者_StackOverflow社区 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");
}
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]];
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
精彩评论