ios - substringWithRange out of bounds--what am I missing? -


i'm trying extract strings code, , i'm coming against nsrangeexception don't understand. here's 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 e.g. location 773, length 29 nsrange endrangeheadword = [stringtoscan rangeofstring:endheadword]; //logs e.g. location 809 nsrange targetrangeheadword; targetrangeheadword.length = endrangeheadword.location - startrangeheadword.location - startrangeheadword.length; //logs e.g. 27 targetrangeheadword.location = startrangeheadword.location + startrangeheadword.length; // logs e.g. 2496 nsstring *translatedheadword = [stringtoscan substringwithrange:targetrangeheadword]; 

adlernumber created , logs without error. trying create translatedheadword (last line of code above) gives me error:

terminating app due uncaught exception 'nsrangeexception', reason: '-[__nscfstring substringwithrange:]: range {2634, 2147481013} out of bounds; string length 9309' 

if targetrangeheadword.length logs 27 , targetrangeheadword.location logs 2496, how can getting numbers nsrangeexception giving me? , how fix this?

thanks in advance help, guys , gals.

add nslogs see more numbers, such stringtoscan.length.

"if targetrangeheadword.length logs 27 , targetrangeheadword.location logs 2496, how can getting numbers nsrangeexception"

try replace targetrangeheadword.location magic number such 2400 , nslog it. 2496 end of stringtoscan ?

-edit: noticed:

terminating app due uncaught exception 'nsrangeexception', reason: '-[__nscfstring substringwithrange:]: range {2634, 2147481013} out of bounds; string length 9309' 

specifically range {2634, 2147481013}, means 1 of closing tags </> not found.

this number "2147481013" indicator not found.


Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

jsf - How to ajax update an item in the footer of a PrimeFaces dataTable? -

django - CSRF verification failed. Request aborted. CSRF cookie not set -