为什么 XCode 给我一个 'unrecognized selector' 错误?

Why is XCode giving me an 'unrecognized selector' error?

我正在使用 Swift 2 和 Xcode 7 开发 Mac 应用程序。

在多年专注于 Ruby 并略微 JavaScript 之后,我才刚刚开始使用 Swift。

我正在创建的 Mac 应用程序包含网络浏览器视图。

我的 ViewController.swift 说:

import Cocoa
import WebKit

class ViewController: NSViewController {

    @IBOutlet var url: NSTextField!
    @IBOutlet var browser: WKWebView!

    @IBAction func go(sender: AnyObject) {

        // browser.loadRequest(NSURLRequest(URL:NSURL(string: url.stringValue)!))
        // above line broken down for debugging

        let url1 = url.stringValue
        print("url1 = \(url1)")

        let url2 = NSURL(string: url1)!
        print("url2 = \(url2)")

        let url3 = NSURLRequest(URL:url2)
        print("url3 = \(url3)")

        print("browser is \(browser)")

        browser.loadRequest(url3)

    }
}

应用构建成功。当我 运行 它时,在 URL 字段中输入 http://apple.com 并单击 Go 按钮,我看到:

url1 = http://apple.com
url2 = http://apple.com
url3 = <NSURLRequest: 0x600000001400> { URL: http://apple.com }
browser is <WebView: 0x608000120e60>
2016-04-06 13:39:51.664 Testivate[6516:427535] -[WebView loadRequest:]: unrecognized selector sent to instance 0x608000120e60
2016-04-06 13:39:51.664 Testivate[6516:427535] -[WebView loadRequest:]: unrecognized selector sent to instance 0x608000120e60
2016-04-06 13:39:51.667 Testivate[6516:427535] (
    0   CoreFoundation                      0x00007fff93f4a4f2 __exceptionPreprocess + 178
    1   libobjc.A.dylib                     0x00007fff8ca7a73c objc_exception_throw + 48
    2   CoreFoundation                      0x00007fff93fb41ad -[NSObject(NSObject) doesNotRecognizeSelector:] + 205
    3   CoreFoundation                      0x00007fff93eba571 ___forwarding___ + 1009
    4   CoreFoundation                      0x00007fff93eba0f8 _CF_forwarding_prep_0 + 120
    5   Testivate                           0x000000010000271a _TFC9Testivate14ViewController2gofPs9AnyObject_T_ + 2154
    6   Testivate                           0x00000001000027b6 _TToFC9Testivate14ViewController2gofPs9AnyObject_T_ + 54
    7   libsystem_trace.dylib               0x00007fff96e1807a _os_activity_initiate + 75
    8   AppKit                              0x00007fff94567e89 -[NSApplication sendAction:to:from:] + 460
    9   AppKit                              0x00007fff94579fde -[NSControl sendAction:to:] + 86
    10  AppKit                              0x00007fff94579f08 __26-[NSCell _sendActionFrom:]_block_invoke + 131
    11  libsystem_trace.dylib               0x00007fff96e1807a _os_activity_initiate + 75
    12  AppKit                              0x00007fff94579e65 -[NSCell _sendActionFrom:] + 144
    13  libsystem_trace.dylib               0x00007fff96e1807a _os_activity_initiate + 75
    14  AppKit                              0x00007fff9457848a -[NSCell trackMouse:inRect:ofView:untilMouseUp:] + 2693
    15  AppKit                              0x00007fff945c0fd0 -[NSButtonCell trackMouse:inRect:ofView:untilMouseUp:] + 744
    16  AppKit                              0x00007fff94576bb4 -[NSControl mouseDown:] + 669
    17  AppKit                              0x00007fff94acb469 -[NSWindow _handleMouseDownEvent:isDelayedEvent:] + 6322
    18  AppKit                              0x00007fff94acc44d -[NSWindow _reallySendEvent:isDelayedEvent:] + 212
    19  AppKit                              0x00007fff9450b63d -[NSWindow sendEvent:] + 517
    20  AppKit                              0x00007fff9448bb3c -[NSApplication sendEvent:] + 2540
    21  AppKit                              0x00007fff942f2ef6 -[NSApplication run] + 796
    22  AppKit                              0x00007fff942bc46c NSApplicationMain + 1176
    23  Testivate                           0x0000000100004f34 main + 84
    24  libdyld.dylib                       0x00007fff9099a5ad start + 1
    25  ???                                 0x0000000000000003 0x0 + 3
)

最初我认为错误是说 loadRequestbrowser 的无法识别的选择器,但你可以看到我已经将函数拆开并且 browser 显然是一个实例WebView,其中 definitely has loadRequest as a method。您可以从我如何拆分方法中看出,我肯定会为 loadRequest 方法提供预期的 NSURLRequest object.

我现在在想,这可能与在Main.storyboard中找不到WebView有关,但它应该可以吗?我在上面定义了这样的连接:

当您查看 Main.storyboard 作为来源时,它会显示:

<webView fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="Ocj-mD-woP">
    <rect key="frame" x="62" y="20" width="406" height="211"/>
    <webPreferences key="preferences" defaultFontSize="12" defaultFixedFontSize="12">
        <nil key="identifier"/>
    </webPreferences>
</webView>

有什么想法吗?谢谢。

您混淆了两种不同的网络视图。

在您的代码中,您说 browserWKWebView。这是一种较新的 Web 视图,最初在 OS X 10.10 中可用。它有一个名为 -loadView: in Objective-C 或 loadView() in Swift 的方法。

@IBOutlet var browser: WKWebView!

但是,当您 运行 代码时,对象实际上是一个 WebView,因为这就是您放入情节提要中的内容。那是一种较旧的网络视图。它没有名为 -loadView: 的方法,因此当您尝试调用它时会出错。

browser is <WebView: 0x608000120e60>
-[WebView loadRequest:]: unrecognized selector sent to instance 0x608000120e60

(再次阅读您链接到的文档。WebView 主框架 实现了 -loadRequest:,但 WebView 没有。 )

这里有两个可能的修复方法:

更简单:修复代码以正确使用 WebView,但故事板保持不变。

@IBOutlet var browser: WebView!
...
browser.mainFrame.loadRequest(url3)

更难: 保留代码使用 WKWebView,但从故事板中删除 WebView

很遗憾 it's currently impossible to put a WKWebView in a storyboard,因此您将不得不编写一些代码来自行设置它。有关如何操作的示例,请参阅链接的答案。

如果您想利用 WKWebView 的新功能,则必须使用第二种选择。