Skip to content

Replace child node #67

Open
Open

Description

@lexiaoyao20

How to replace child node in parent node?

Activity

  1. lexiaoyao20 commented on Apr 17, 2016

    @lexiaoyao20
    Author

    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
    
    
  2. nolanw commented on May 7, 2016

    @nolanw
    Owner

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions