【发布时间】: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 中制作的(我也遇到了一些其他兼容性问题)。
【问题讨论】: