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).
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
.
Swift 3
replace the following code
.LeftMouseDownMask | .RightMouseDownMask
with this
[NSEventMask.leftMouseDown, NSEventMask.rightMouseDown]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With