How to make a custom NSFormatter work correctly on Snow Leopard?

Posted by Nathan on Stack Overflow See other posts from Stack Overflow or by Nathan
Published on 2009-10-29T19:22:45Z Indexed on 2010/05/28 23:12 UTC
Read the original article Hit count: 340

Filed under:
|
|

I have a custom NSFormatter attached to several NSTextFields who's only purpose is to uppercase the characters as they are typed into field. The entire code for my formatter is included below.

The stringForObjectValue() and getObjectValue() implementations are no-ops and taken pretty much directly out of Apple's documentation. I'm using the isPartialStringValid() method to return an uppercase version of the string. This code works correctly in 10.4 and 10.5. When I run it on 10.6, I get "strange" behaviour where text fields aren't always render the characters that are typed and sometimes are just displaying garbage. I've tried enabling NSZombie detection and running under Instruments but nothing was reported. I see errors like the following in "Console":

HIToolbox: ignoring exception '*** -[NSCFString replaceCharactersInRange:withString:]: Range or index out of bounds' that raised inside Carbon event dispatch
(
    0   CoreFoundation                      0x917ca58a __raiseError + 410
    1   libobjc.A.dylib                     0x94581f49 objc_exception_throw + 56
    2   CoreFoundation                      0x917ca2b8 +[NSException raise:format:arguments:] + 136
    3   CoreFoundation                      0x917ca22a +[NSException raise:format:] + 58
    4   Foundation                          0x9140f528 mutateError + 218
    5   AppKit                              0x9563803a -[NSCell textView:shouldChangeTextInRange:replacementString:] + 852
    6   AppKit                              0x95636cf1 -[NSTextView(NSSharing) shouldChangeTextInRanges:replacementStrings:] + 1276
    7   AppKit                              0x95635704 -[NSTextView insertText:replacementRange:] + 667
    8   AppKit                              0x956333bb -[NSTextInputContext handleTSMEvent:] + 2657
    9   AppKit                              0x95632949 _NSTSMEventHandler + 209
    10  HIToolbox                           0x93379129 _ZL23DispatchEventToHandlersP14EventTargetRecP14OpaqueEventRefP14HandlerCallRec + 1567
    11  HIToolbox                           0x933783f0 _ZL30SendEventToEventTargetInternalP14OpaqueEventRefP20OpaqueEventTargetRefP14HandlerCallRec + 411
    12  HIToolbox                           0x9339aa81 SendEventToEventTarget + 52
    13  HIToolbox                           0x933fc952 SendTSMEvent + 82
    14  HIToolbox                           0x933fc2cf SendUnicodeTextAEToUnicodeDoc + 700
    15  HIToolbox                           0x933fbed9 TSMKeyEvent + 998
    16  HIToolbox                           0x933ecede TSMProcessRawKeyEvent + 2515
    17  AppKit                              0x95632228 -[NSTextInputContext handleEvent:] + 1453
    18  AppKit                              0x9562e088 -[NSView interpretKeyEvents:] + 209
    19  AppKit                              0x95631b45 -[NSTextView keyDown:] + 751
    20  AppKit                              0x95563194 -[NSWindow sendEvent:] + 5757
    21  AppKit                              0x9547bceb -[NSApplication sendEvent:] + 6431
    22  AppKit                              0x9540f6fb -[NSApplication run] + 917
    23  AppKit                              0x95407735 NSApplicationMain + 574
    24  macsetup                            0x00001f9f main + 24
    25  macsetup                            0x00001b75 start + 53
)

Can anybody shed some light on what is happening? Am I just using NSFormatter incorrectly?

-(NSString*) stringForObjectValue:(id)object {
    if( ![object isKindOfClass: [ NSString class ] ] ) {
    	return nil;
    }
    return [ NSString stringWithString: object ];
}

-(BOOL)getObjectValue: (id*)object forString: string errorDescription:(NSString**)error {
    if( object ) {
    	*object = [ NSString stringWithString: string ];
    	return YES;
    }
    return NO;
}

-(BOOL) isPartialStringValid: (NSString*) cStr newEditingString: (NSString**) nStr errorDescription: (NSString**) error {
    *nStr = [NSString stringWithString: [cStr uppercaseString]];
    return NO;
}

© Stack Overflow or respective owner

Related posts about macosx

Related posts about snow-leopard