【问题标题】:[NSSearchField object]: unrecognized selector sent to instance[NSSearchField 对象]:发送到实例的无法识别的选择器
【发布时间】:2014-08-03 08:21:57
【问题描述】:

我正在一个简单的 Mac 应用程序中测试 Swift。我在 Storyboard 中有一个 NSToolbar 并在里面画了一个 NSSearchfield。 NSSearchfiled 连接到 First Responder 的方法 controlTextDidChange(第一响应者是我添加 NSTextFieldDelegate 的 ViewController)。

这是方法:

@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
)

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

【问题讨论】:

  • 试试这个@IBAction override func controlTextDidChange(obj: NSNotification!) { println("searching...") let lSearchField: NSSearchField = obj.object; println(lSearchField.stringValue) }
  • 感谢您的建议。不幸的是问题仍然存在
  • 我想我在 NSToolbar 中拥有 NSSearchField 中缺少一些东西,而 NSToolbar 又在 NSWindow 中,这是 Swift 和 Storyboard 强制执行的方式,而 controlTextDidChange 方法(在 Swift 文档中甚至没有涉及)在 ViewController 中。

标签: macos swift xcode6 nssearchfield


【解决方案1】:

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)
}

【讨论】:

  • 这是 NSNotification... extension NSObject { func controlTextDidBeginEditing(obj: NSNotification!) func controlTextDidEndEditing(obj: NSNotification!) func controlTextDidChange(obj: NSNotification!) }
  • 是的。 .../ViewController.swift:40:29:使用选择器“controlTextDidChange:”覆盖方法具有不兼容的类型“(NSSearchField!)->()”
  • 不如尝试用其他方法连接IBAction。或者使用委托方法而不是 IBAction。
【解决方案2】:

通过定义自定义方法解决

@IBAction func controlTextDidChange_Custom(obj: NSSearchField!) {
    if (!obj.stringValue.isEmpty) {
        println("Searched: \(obj.stringValue)")
    } else {
        println("EMPTY")
    }
}

【讨论】:

    猜你喜欢
    • 2019-08-28
    • 2012-07-24
    相关资源
    最近更新 更多