我从下面的代码中收到以下错误,我不知道为什么。
致命异常 NSRangeException -[__NSCFString replaceOccurrencesOfString:withString:options:range:]: Range {0, 7} out of bounds; 字符串长度 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];
}
}