如何使自定义 NSFormatter 在 Snow Leopard 上正常工作?
我有一个自定义的 NSFormatter 附加到几个 NSTextFields 上,其唯一目的是在将字符输入字段时将其大写。我的格式化程序的完整代码包含在下面。
stringForObjectValue()
和 getObjectValue()
实现是无操作的,并且几乎直接取自 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;
}
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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在我的所有
isPartialStringValid:newEditingString:errorDescription:
方法中,我初始化了*error = nil
。可能很重要。In all my
isPartialStringValid:newEditingString:errorDescription:
methods, I initialize*error = nil
. Could be important.