gpt4 book ai didi

macos - NSTextField 在 Swift 中绑定(bind)为 double,如何允许空白?

转载 作者:行者123 更新时间:2023-12-03 17:46:38 26 4
gpt4 key购买 nike

我正在使用 NSTextField,我已通过 Storyboard 中的绑定(bind)将其绑定(bind)到整数。 Swift 代码如下所示:

@objc dynamic var quantity: Int = 0

这对于验证数字非常有用,如果尝试输入无效的内容(例如字母),我会收到一条有用的消息。

我遇到的问题是,当您在未填写任何内容的情况下跳出字段时,应用程序会崩溃。根据错误消息,最相关的似乎是:

2019-03-09 16:29:46.813928+0000 ViewWebSocketLearning[34535:1969855] [General] [<ViewWebSocketLearning.OrderFormViewController 0x103131820> setNilValueForKey]: could not set nil as the value for the key show.
2019-03-09 16:29:46.825146+0000 ViewWebSocketLearning[34535:1969855] [General] (
0 CoreFoundation 0x00007fff3d7b6ded __exceptionPreprocess + 256
1 libobjc.A.dylib 0x00007fff69882720 objc_exception_throw + 48
2 CoreFoundation 0x00007fff3d7b6c1f +[NSException raise:format:] + 201
3 Foundation 0x00007fff3fbe8dbb -[NSObject(NSKeyValueCoding) setNilValueForKey:] + 81
4 Foundation 0x00007fff3fac1450 -[NSObject(NSKeyValueCoding) setValue:forKey:] + 331
5 Foundation 0x00007fff3faec38a -[NSObject(NSKeyValueCoding) setValue:forKeyPath:] + 271
6 AppKit 0x00007fff3af0bf03 -[NSBinder _setValue:forKeyPath:ofObject:mode:validateImmediately:raisesForNotApplicableKeys:error:] + 473
7 AppKit 0x00007fff3af0bccf -[NSBinder setValue:forBinding:error:] + 236
8 AppKit 0x00007fff3b54c376 -[NSValueBinder _applyObjectValue:forBinding:canRecoverFromErrors:handleErrors:typeOfAlert:discardEditingCallback:otherCallback:callbackContextInfo:didRunAlert:] + 225
9 AppKit 0x00007fff3b54c698 -[NSValueBinder applyDisplayedValueHandleErrors:typeOfAlert:canRecoverFromErrors:discardEditingCallback:otherCallback:callbackContextInfo:didRunAlert:error:] + 544
10 AppKit 0x00007fff3b54c81a -[NSValueBinder _applyDisplayedValueIfHasUncommittedChangesWithHandleErrors:typeOfAlert:discardEditingCallback:otherCallback:callbackContextInfo:didRunAlert:error:] + 105
11 AppKit 0x00007fff3b053e4e -[NSValueBinder validateAndCommitValueInEditor:editingIsEnding:errorUserInterfaceHandled:] + 460
12 AppKit 0x00007fff3b053c5a -[_NSBindingAdaptor _validateAndCommitValueInEditor:editingIsEnding:errorUserInterfaceHandled:bindingAdaptor:] + 175
13 AppKit 0x00007fff3b053b8d -[_NSBindingAdaptor validateAndCommitValueInEditor:editingIsEnding:errorUserInterfaceHandled:] + 240
14 AppKit 0x00007fff3af83e2b -[NSTextField textShouldEndEditing:] + 368
15 AppKit 0x00007fff3af44cc9 -[NSTextView(NSSharing) resignFirstResponder] + 499
16 AppKit 0x00007fff3ada2522 -[NSWindow _realMakeFirstResponder:] + 258
17 AppKit 0x00007fff3b058001 -[NSTextView(NSPrivate) _giveUpFirstResponder:] + 263

我将此解释为 NSTextField 绑定(bind)不接受 nil 条目,这显然是由于将该字段留空所致。

如何防止此异常发生?对于我的应用程序,在处理其他事情时将该字段留空是完全可以的。

最佳答案

通过将 NSTextFields 与绑定(bind)一起使用,Cocoa 使用键值编码方法 (KVC),例如 setValue:forKey: Storyboard/XIB 中的绑定(bind)设置始终与 NSObject 子类对象( Controller )绑定(bind)。非对象值经过特殊处理:

If your key-value coding compliant object receives a setValue:forKey: message with nil passed as the value for a non-object property, the default implementation has no appropriate, generalized course of action. It therefore sends itself a setNilValueForKey: message, which you can override. The default implementation of setNilValueForKey: raises an NSInvalidArgumentException exception, but you can provide an appropriate, implementation-specific behavior.

https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/KeyValueCoding/HandlingNon-ObjectValues.html#//apple_ref/doc/uid/10000107i-CH5-SW1

在您的情况下 OrderFormViewController 没有重写 setNilValueForKey: 方法,并且引发了异常。

实现setNilValueForKey:将解决所有问题。

PS:使用 NSNumberFormatter 或绑定(bind)到 NSNumber 对象也可以解决该问题。

关于macos - NSTextField 在 Swift 中绑定(bind)为 double,如何允许空白?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55079595/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com