开发者

NSNumberFormatter subclass working in 10.6 but failing in 10.5 with generic error

开发者 https://www.devze.com 2022-12-16 17:48 出处:网络
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

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消