0

I have this code:

    for (TFHppleElement *element in htmlNodes) {

    if (![element.tagName isEqual:@"img"]) {

        NSString *elementHtmlCode = htmlCode;

        if (element.text != nil) {

            NSString *elementText = element.text;

            NSRange textRange = [elementHtmlCode rangeOfString:elementText];

            elementHtmlCode = [elementHtmlCode stringByReplacingCharactersInRange:textRange withString:word];

            [htmlCode setString:elementHtmlCode];

            for (TFHppleElement *child in element.children) {

                if (child.text != nil) {

                    NSString *childText = child.text;

                    NSString *childHtmlCode = htmlCode;

                    NSRange textRange = [childHtmlCode rangeOfString:childText];

                    childHtmlCode = [childHtmlCode stringByReplacingCharactersInRange:textRange withString:word];

                    [htmlCode setString:childHtmlCode];

                }
            }
        }
    }
}

My problem was after the first complete loop is complete and when enters again on:

NSRange textRange = [elementHtmlCode rangeOfString:elementText];
elementHtmlCode = [elementHtmlCode stringByReplacingCharactersInRange:textRange withString:word];

I get 0 on textRange.length.

I have setup a breakpoint on this line and I can see the value of length changing to 0 after process run this line.

What I'm missing?

Thanks!

Samael
  • 21
  • 4

1 Answers1

0

rangeOfString: returns a {NSNotFound, 0} rect if it didn't found the specified string in the receiver. Check the two objects' value elementHtmlCode and elementText at first. Refer to the context of your code, it's possible with any values for the two strings. Enjoy!

Itachi
  • 5,777
  • 2
  • 37
  • 69