以下のコードから次のエラーが発生しましたが、その理由がわかりません。
致命的な例外 NSRangeException -[__NSCFString replaceOccurrencesOfString:withString:options:range:]: 範囲 {0, 7} が範囲外です。文字列の長さ 6
誰かが説明できるのだろうか?
異なる文字列の長さに対応するために、新しい NSRange 変数が必要なだけですか?
+(double)removeFormatPrice:(NSString *)strPrice {
NSNumberFormatter *currencyFormatter = [[NSNumberFormatter alloc] init];
[currencyFormatter setNumberStyle:NSNumberFormatterCurrencyStyle];
NSNumber* number = [currencyFormatter numberFromString:strPrice];
//cater for commas and create double to check against the number put
//through the 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 is not a decimal point replace it with a 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];
}
}