我正在学习使用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)
        }
    }


}

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

最佳答案

解决了该问题。

我必须将eventMonitor = EventMonitor(mask: .LeftMouseDownMask | .RightMouseDownMask) { [unowned self] event in行更改为eventMonitor = EventMonitor(mask: [.LeftMouseDownMask, .RightMouseDownMask]) { [unowned self] event in

关于macos - EventMonitor .LeftMouseDownMask表达式类型不明确,没有更多上下文,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/33462970/

10-15 11:56