我正在一个简单的Mac应用程序中测试Swift。我在故事板上画了一个nstoolbar,在里面画了一个nssearchfield。nssearchfield连接到第一个响应程序的方法controltextdidchange(第一个响应程序是viewcontroller,我在其中添加了nstextfieldDelegate)。
方法如下:

@IBAction override func controlTextDidChange(obj: NSNotification!) {
    println("searching...")
    println(obj.object.stringValue)
}

每次搜索新字符时都会正确调用该方法,并且应用程序不会崩溃,但返回的内容如下:
searching...
2014-08-03 09:56:57.770 TestApp[1129:24219] -[NSSearchField object]: unrecognized selector sent to instance 0x6080001a07e0
2014-08-03 09:56:57.770 TestApp[1129:24219] -[NSSearchField object]: unrecognized selector sent to instance 0x6080001a07e0
2014-08-03 09:56:57.775 TestApp[1129:24219] (
0   CoreFoundation                      0x00007fff92e6af1c __exceptionPreprocess + 172
1   libobjc.A.dylib                     0x00007fff930ae74e objc_exception_throw + 43
2   CoreFoundation                      0x00007fff92e6de4d -[NSObject(NSObject) doesNotRecognizeSelector:] + 205
3   CoreFoundation                      0x00007fff92db63c4 ___forwarding___ + 1028
4   CoreFoundation                      0x00007fff92db5f38 _CF_forwarding_prep_0 + 120
5   TestApp                           0x000000010000c187 _TFC9TestApp14ViewController20controlTextDidChangefS0_FGSQCSo14NSNotification_T_ + 231
6   TestApp                           0x000000010000c582 _TToFC9TestApp14ViewController20controlTextDidChangefS0_FGSQCSo14NSNotification_T_ + 66
7   libsystem_trace.dylib               0x00007fff9117bc07 _os_activity_initiate + 75
8   AppKit                              0x00007fff8d52b168 -[NSApplication sendAction:to:from:] + 410
9   AppKit                              0x00007fff8d52af90 -[NSControl sendAction:to:] + 86
10  AppKit                              0x00007fff8d6faf91 __26-[NSCell _sendActionFrom:]_block_invoke + 131
11  libsystem_trace.dylib               0x00007fff9117bc07 _os_activity_initiate + 75
12  AppKit                              0x00007fff8d57329e -[NSCell _sendActionFrom:] + 144
13  AppKit                              0x00007fff8d92fe8f __64-[NSSearchFieldCell(NSSearchFieldCell_Local) _sendPartialString]_block_invoke + 63
14  libsystem_trace.dylib               0x00007fff9117bc07 _os_activity_initiate + 75
15  AppKit                              0x00007fff8d92fe47 -[NSSearchFieldCell(NSSearchFieldCell_Local) _sendPartialString] + 186
16  Foundation                          0x00007fff932ee3d3 __NSFireTimer + 95
17  CoreFoundation                      0x00007fff92dbf464 __CFRUNLOOP_IS_CALLING_OUT_TO_A_TIMER_CALLBACK_FUNCTION__ + 20
18  CoreFoundation                      0x00007fff92dbf0f3 __CFRunLoopDoTimer + 1059
19  CoreFoundation                      0x00007fff92e320fd __CFRunLoopDoTimers + 301
20  CoreFoundation                      0x00007fff92d7b4d2 __CFRunLoopRun + 2018
21  CoreFoundation                      0x00007fff92d7aaa8 CFRunLoopRunSpecific + 296
22  HIToolbox                           0x00007fff90adcaff RunCurrentEventLoopInMode + 235
23  HIToolbox                           0x00007fff90adc872 ReceiveNextEventCommon + 431
24  HIToolbox                           0x00007fff90adc6b3 _BlockUntilNextEventMatchingListInModeWithFilter + 71
25  AppKit                              0x00007fff8d35c2a5 _DPSNextEvent + 1000
26  AppKit                              0x00007fff8d35ba79 -[NSApplication nextEventMatchingMask:untilDate:inMode:dequeue:] + 139
27  AppKit                              0x00007fff8d34fad3 -[NSApplication run] + 594
28  AppKit                              0x00007fff8d33b2de NSApplicationMain + 1778
29  TestApp                           0x000000010000da72 top_level_code + 34
30  TestApp                           0x000000010000daaa main + 42
31  libdyld.dylib                       0x00007fff8cb765c9 start + 1
32  ???                                 0x0000000000000003 0x0 + 3
)

我不知道“无法识别的选择器发送到实例”来自何处。

最佳答案

controlTextDidChange方法需要NSSearchField!,而不是NSNotification!
更换

@IBAction override func controlTextDidChange(obj: NSNotification!) {
    println("searching...")
    println(obj.object.stringValue)
}

具有
@IBAction override func controlTextDidChange(obj: NSSearchField!) {
    println("searching...")
    println(obj.stringValue)
}

10-06 05:06