I'm subclassing NSNumberFormatter to allow the currency symbol to be optional. Here's my code:
- (BOOL)getObjectValue:(out id *)anObject forString:(NSString *)aString range:(inout NSRange *)rangep error:(out NSError **)error {
if (rangep == NULL){
NSRange newRange = NSMakeRange(0, [aString length]);
rangep = &newRange;
}
NSString *newStr = [aString stringByReplacingOccurrencesOfString:[self currencySymbol]
withString:@""
options:0
range:*rangep];
if (![newStr isEqualToString:aString]) {
NSRange newRange;
newRange.location = ((NSRange)*rangep).location;
newRange.length = ((NSRange)*rangep).length;
newRange.length -= ([aString length] - [newStr length]);
rangep = &开发者_如何学Goamp;newRange;
}
return [super getObjectValue:anObject
forString:newStr
range:rangep
error:error];
}
On Mac OS X 10.6 and higher, this code works absolutely fine. However, on 10.5.x, the code fails — the call to [super getObjectValue:...] fails, and the error returned is the rather generic "NSFormattingError" (code 2048).
Does anyone know what's going on?
The formatter is set to generate decimal numbers.
精彩评论