ios - Fatal Exception NSRangeException -[__NSCFString replaceOccurrencesOfString:withString:options:range:]: Range {N, N} out of bounds; string length N -


i'm getting following error code below , i'm not sure why.

fatal exception nsrangeexception -[__nscfstring replaceoccurrencesofstring:withstring:options:range:]: range {0, 7} out of bounds; string length 6

i wondered if explain ?

is need new nsrange variable cater different string length ?

+(double)removeformatprice:(nsstring *)strprice {      nsnumberformatter *currencyformatter = [[nsnumberformatter alloc] init];     [currencyformatter setnumberstyle:nsnumberformattercurrencystyle];     nsnumber* number = [currencyformatter numberfromstring:strprice];      //cater commas , create double check against number put      //through currency formatter     nsnumberformatter *formatter = [[nsnumberformatter alloc] init];     nsmutablestring *mstring = [nsmutablestring stringwithstring:strprice];     nsrange wholeshebang = nsmakerange(0, [mstring length]);      [mstring replaceoccurrencesofstring: [formatter groupingseparator]                              withstring: @""                                 options: 0                                   range: wholeshebang];      //if decimal symbol not decimal point replace decimal point     nsstring *symbol = [[nslocale currentlocale]                    objectforkey:nslocaledecimalseparator];     if (![symbol isequaltostring:@"."]) {         [mstring replaceoccurrencesofstring: symbol                                  withstring: @"."                                     options: 0                                       range: wholeshebang]; // error here     }      double newprice = [mstring doublevalue];     if (number == nil) {         return newprice;     } else {         return [number doublevalue];     } } 

after first replacement operation has been done, string shorter was, when constructed range (you replacing characters nothing). original initialisation

nsrange wholeshebang = nsmakerange(0, [mstring length]); 

gives range, length large. example:

nsmutablestring* str = [nsmutablestring stringwithstring: @"1.234,5"]; nsrange allofstr = nsmakerange(0, [str length]);  [str replaceoccurrencesofstring: @"."                          withstring: @""                             options: 0                               range: allofstr]; 

note, str looks 1234,5, i.e., shorter 1 character @ time, range initialized.

for reason, index out of bounds error, if use range again on short string. should re-initialize range before passing second replace op.:

allofstr = nsmakerange(0, [str length]);  [str replaceoccurrencesofstring: @","                          withstring: @"."                             options: 0                               range: allofstr]; 

Comments

Popular posts from this blog

c++ - CryptStringToBinary API behavior -

iphone - Three second countdown in cocos2d -

java.util.scanner - How to read and add only numbers to array from a text file -