0

I'm trying to extract some strings from some code, and I'm coming up against an NSRangeException I don't understand. Here's my code:

NSString *start = @"<strong><span class='target'>";
NSString *end = @"</span></strong> ";

NSRange startRange = [stringToScan rangeOfString:start];
NSRange endRange = [stringToScan rangeOfString:end];
NSRange targetRange;
targetRange.length = endRange.location - startRange.location - startRange.length;
targetRange.location = startRange.location + startRange.length;
NSString *adlerNumber = [stringToScan substringWithRange:targetRange];
NSLog(@"%@",adlerNumber);

NSString *startHeadword = @"Translated headword: </strong>";
NSString *endHeadword = @"<strong class=";

NSRange startRangeHeadword = [stringToScan rangeOfString:startHeadword]; //logs as e.g. location 773, length 29
NSRange endRangeHeadword = [stringToScan rangeOfString:endHeadword]; //logs as e.g. location 809
NSRange targetRangeHeadword;
targetRangeHeadword.length = endRangeHeadword.location - startRangeHeadword.location - startRangeHeadword.length; //logs as e.g. 27
targetRangeHeadword.location = startRangeHeadword.location + startRangeHeadword.length; // logs as e.g. 2496
NSString *translatedHeadword = [stringToScan substringWithRange:targetRangeHeadword];

adlerNumber is created and logs without error. But then trying to create translatedHeadword (last line of code above) gives me this error:

Terminating app due to uncaught exception 'NSRangeException', reason: '-[__NSCFString substringWithRange:]: Range {2634, 2147481013} out of bounds; string length 9309'

If targetRangeHeadword.length logs as 27 and targetRangeHeadword.location logs as 2496, how can I be getting the numbers the NSRangeException is giving me? And how do I fix this?

Thanks in advance for your help, guys and gals.

Joel Derfner
  • 2,207
  • 6
  • 33
  • 45
  • Also, make sure this: `class='target'` is not actually reading this: `class="target"`. For `NSString`, `(' != ")` – bauerMusic Jun 28 '14 at 05:21

3 Answers3

1

Add some NSLogs to see more numbers, such as stringToScan.length.

"If targetRangeHeadword.length logs as 27 and targetRangeHeadword.location logs as 2496, how can I be getting the numbers the NSRangeException"

Try to replace targetRangeHeadword.location with a magic number such as 2400 and NSLog it. Could it be that 2496 is the end of stringToScan ?

-EDIT: I just noticed:

Terminating app due to uncaught exception 'NSRangeException', reason: '-[__NSCFString substringWithRange:]: Range {2634, 2147481013} out of bounds; string length 9309'

Specifically Range {2634, 2147481013}, means one of your closing tags </> are not found.

This number "2147481013" is an indicator that something was not found.

bauerMusic
  • 5,470
  • 5
  • 38
  • 53
0

startRangeHeadword was not found, it's location was NSNotFound or possibly another search returned NSNotFound. You must check that NSRange location is not NSNotFound after any search (rangeOfString). Error checking is a pain but really must be done.

Single step through your code and watch the range location for NSNotFound (a large number).

As an alternative consider NSScanner, it is probably better suited to your needs.

zaph
  • 111,848
  • 21
  • 189
  • 228
  • Thanks--but before I posted this I did single step through the code, and all the ranges logged locations--I think I commented the code above with what the locations were. :( But I'll go check out NSScanner. – Joel Derfner Jun 28 '14 at 03:40
  • When you get a location or range like 2147481013 there is a problem and it is usually due to NSNotFound that on 32 bit system is 2147483647. In any event a length of 2147481013 it most likely an error, find where that come from. – zaph Jun 28 '14 at 11:42
0

I would consider using NSRegularExpressions here coupled with the rangeOfString method. That way you can grab all of the instances of a particular pattern and replace them with whatever you want or extract them. A good site to practice these is here: www.regexr.com

arc4randall
  • 3,275
  • 3
  • 27
  • 40