【问题标题】:EventMonitor .LeftMouseDownMask Type of expression is ambiguous without more contextEventMonitor .LeftMouseDownMask 表达式类型不明确,没有更多上下文
【发布时间】:2015-11-01 14:01:28
【问题描述】:

我正在学习使用 Swift 2 为 Xcode 制作状态栏应用程序。我几乎完成了这个tutorial,但是在eventMonitor = EventMonitor(mask: . | .RightMouseDownMask) { [unowned self] event in.LeftMouseDownMask 线上给我一个错误,说Type of expression is ambiguous without more context。我将如何解决这种类型的表达问题?

这是我的 AppDelegate.swift 文件:

import Cocoa

@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {

    @IBOutlet weak var window: NSWindow!

    //Event Monitering
    var eventMonitor: EventMonitor?
    ///////////////////
    let statusItem = NSStatusBar.systemStatusBar().statusItemWithLength(-2)
    let popover = NSPopover()


    func applicationDidFinishLaunching(notification: NSNotification) {
        if let button = statusItem.button {
            button.image = NSImage(named: "StatusBarButtonImage")
            button.action = Selector("togglePopover:")
        }

        popover.contentViewController = QuotesViewController(nibName: "QuotesViewController", bundle: nil)

        //Event Monitering
        eventMonitor = EventMonitor(mask: .LeftMouseDownMask | .RightMouseDownMask) { [unowned self] event in
            if self.popover.shown {
                self.closePopover(event)
            }
        }
        eventMonitor?.start()
        //////////////////////
    }

    func applicationWillTerminate(aNotification: NSNotification) {
        // Insert code here to tear down your application
    }


    func showPopover(sender: AnyObject?) {
        if let button = statusItem.button {
            popover.showRelativeToRect(button.bounds, ofView: button, preferredEdge: NSRectEdge.MinY)
        }
    }

    func closePopover(sender: AnyObject?) {
        popover.performClose(sender)
    }

    func togglePopover(sender: AnyObject?) {
        if popover.shown {
            closePopover(sender)
        } else {
            showPopover(sender)
        }
    }


}

我猜这个错误是因为.LeftMouseDownMask 已经在 Swift 2 中更改为其他内容,因为 tutorial 是在 Swift 1 中制作的(我也遇到了一些其他兼容性问题)。

【问题讨论】:

    标签: macos swift2 toolbar


    【解决方案1】:

    解决了这个问题。

    我不得不将eventMonitor = EventMonitor(mask: .LeftMouseDownMask | .RightMouseDownMask) { [unowned self] event in 改为eventMonitor = EventMonitor(mask: [.LeftMouseDownMask, .RightMouseDownMask]) { [unowned self] event in

    【讨论】:

      【解决方案2】:

      斯威夫特 3

      替换以下代码

      .LeftMouseDownMask | .RightMouseDownMask
      

      有了这个

      [NSEventMask.leftMouseDown, NSEventMask.rightMouseDown]
      

      【讨论】:

      • 谢谢@quemeful。现在是NSEvent.EventTypeMask.leftMouseDown
      猜你喜欢
      • 1970-01-01
      • 2021-05-17
      • 2019-04-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-04-30
      • 2019-02-01
      • 2019-04-22
      相关资源
      最近更新 更多