0
我有一個自定義的NSFormatter附加到幾個NSTextFields誰是唯一的目的是大寫的字符,因爲他們被輸入到字段。我的格式化程序的完整代碼包含在下面。如何使自定義NSFormatter在Snow Leopard上正常工作?
stringForObjectValue()
和getObjectValue()
實現是no-ops,幾乎直接從Apple的文檔中取出。我正在使用isPartialStringValid()
方法返回字符串的大寫版本。此代碼在10.4和10.5中正常工作。當我在10.6上運行它時,出現「奇怪」的行爲,其中文本字段並不總是呈現鍵入的字符,有時僅顯示垃圾。我試過啓用NSZombie檢測並運行在Instruments下,但沒有任何報道。我在「控制檯」中看到如下錯誤:
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
)
任何人都可以對發生的事情有所瞭解嗎?我只是不正確地使用NSFormatter?
-(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;
}
你是否得到這個工作?我試圖做同樣的事情,偶然發現你的代碼,將它複製/粘貼到我的應用程序中,並且它順利運行。 – rogerl 2011-05-04 21:31:58