NSTextField 在 Swift 中绑定为 double,如何允许空白?

NSTextField bound to double in Swift, how to allow blank?

我正在使用 NSTextField,我已通过情节提要中的绑定将其绑定到一个整数。 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 绑定不接受 nil 条目,显然是由于将该字段留空造成的。

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

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

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 或绑定到 NSNumber 对象也可以解决问题。