Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EventMonitor .LeftMouseDownMask Type of expression is ambiguous without more context

I am learning to make a status bar application for Xcode using Swift 2. I have nearly completed this tutorial, however on the line eventMonitor = EventMonitor(mask: . | .RightMouseDownMask) { [unowned self] event in, .LeftMouseDownMask gives me an error saying Type of expression is ambiguous without more context. How would I fix this Type of expression issue?

Here is my AppDelegate.swift file:

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


}

I am guessing that this error is because .LeftMouseDownMask has changed to something else in Swift 2 since the tutorial was made in Swift 1 (and I had a few other compatibility issues too).

like image 897
iProgram Avatar asked Nov 01 '15 14:11

iProgram


2 Answers

Fixed the issue.

I had to change the line eventMonitor = EventMonitor(mask: .LeftMouseDownMask | .RightMouseDownMask) { [unowned self] event in to eventMonitor = EventMonitor(mask: [.LeftMouseDownMask, .RightMouseDownMask]) { [unowned self] event in.

like image 59
iProgram Avatar answered Nov 07 '22 01:11

iProgram


Swift 3

replace the following code

.LeftMouseDownMask | .RightMouseDownMask

with this

[NSEventMask.leftMouseDown, NSEventMask.rightMouseDown]
like image 25
quemeful Avatar answered Nov 07 '22 01:11

quemeful