gpt4 book ai didi

swift - Swift 4 中的选择器实现

转载 作者:行者123 更新时间:2023-11-30 12:00:57 25 4
gpt4 key购买 nike

我正在尝试定义 NSRect 跟踪。在一个新项目中,我只是向 ViewController.swift 文件添加几行:

import Cocoa

class ViewController: NSViewController {
let tester = Test()
override func viewDidLoad() {
super.viewDidLoad()
view.addTrackingRect(view.bounds, owner: tester, userData: nil, assumeInside: false)
}
}

class Test: NSObject {
@objc func mouseEntered(with event: NSEvent) {
print("mouseEntered")
}
@objc func mouseExited(with event: NSEvent) {
print("mouseExited")
}
override func responds(to aSelector: Selector!) -> Bool {
print("aSelector: -> \(aSelector) -> \(super.responds(to: aSelector))")
return super.responds(to: aSelector)
}
}

结果是应用程序崩溃:

aSelector: -> Optional(mouseEntered:) -> false 2017-11-12 00:18:56.204723+0200 test21[19694:1744132] *** Assertion failure in -[NSWindow _setTrackingRect:inside:owner:userData:useTrackingNum:install:], /BuildRoot/Library/Caches/com.apple.xbs/Sources/AppKit/AppKit-1561.10.101/AppKit.subproj/winTrackingAreas.m:408 2017-11-12 00:18:56.207360+0200 test21[19694:1744132] [General] An uncaught exception was raised 2017-11-12 00:18:56.207382+0200 test21[19694:1744132] [General] Window: target of tracking rect doesnt understand -mouseEntered: or -mouseExited messages

所以我的问题是 - 如何定义一个有效的选择器???

我。

最佳答案

addTrackingRect(_:owner:userData:assumeInside:) 的 Swift 文档相当具有误导性;它说所有者应该回应

to both mouseEntered(with:) and mouseExited(with:).

但是,它真正应该说的是它需要响应选择器mouseEntered:mouseExited:,因为默认情况下, mouseEntered(with:) 等方法的选择器是 mouseEnteredWith:,因为参数标签是签名的一部分。

因此,要解决此问题,您可以:

  1. 删除参数标签:

    class Test : NSObject {
    @objc func mouseEntered(_ event: NSEvent) {
    print("mouseEntered")
    }

    @objc func mouseExited(_ event: NSEvent) {
    print("mouseExited")
    }
    // ...
    }
  2. 手动定义选择器:

    class Test : NSObject {
    @objc(mouseEntered:) func mouseEntered(with event: NSEvent) {
    print("mouseEntered")
    }

    @objc(mouseExited:) func mouseExited(with event: NSEvent) {
    print("mouseExited")
    }
    // ...
    }

关于swift - Swift 4 中的选择器实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47243385/

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