HTMLReader
HTMLReader copied to clipboard
Replace child node
How to replace child node in parent node?
I have create a category.
@interface HTMLNode (Replace)
- (void)replaceChildNode:(HTMLNode *)node withNode:(HTMLNode *)newNode;
- (void)replaceChildNodeAtIndex:(NSUInteger)index withNode:(HTMLNode *)newNode;
@end
@implementation HTMLNode (Replace)
- (void)replaceChildNode:(HTMLNode *)node withNode:(HTMLNode *)newNode {
NSUInteger index = [self.mutableChildren indexOfObject:node];
if (index == NSNotFound) {
return;
}
[self replaceChildNodeAtIndex:index withNode:newNode];
}
- (void)replaceChildNodeAtIndex:(NSUInteger)index withNode:(HTMLNode *)newNode {
if (self.mutableChildren.count <= index) {
return;
}
[self.mutableChildren replaceObjectAtIndex:index withObject:newNode];
}
@end
Hello! I see you've answered your own question, and that's the right way to go about it. I'm guessing you're not the only one wondering, and in either case some convenience methods would be helpful. I'll see about adding these methods to the project.