我一直在尝试在 Swift 的 Objective-C 中为我正在玩的一个新的 swift 应用程序复制执行此操作的常用方法。
如何在 Objective-C 中执行此操作有详细记录,您可以获得共享的苹果事件管理器并调用 setEventHandler
将函数注册为事件类处理程序的方法 kInternetEventClass
事件 ID kAEGetURL
.
所以在 swift 中,我尝试将这段代码添加到一个全新项目中的模板 AppDelegate.swift 中来做同样的事情:
func applicationWillFinishLaunching(aNotification: NSNotification?) {
var appleEventManager:NSAppleEventManager = NSAppleEventManager.sharedAppleEventManager()
appleEventManager.setEventHandler(self, andSelector: "handleGetURLEvent:withReplyEvent:", forEventClass: kInternetEventClass, andEventID: kAEGetURL)
}
func handleGetURLEvent(event: NSAppleEventDescriptor?, replyEvent: NSAppleEventDescriptor?) {
println("yay");
}
据我所知,这只是标准 Objective-c 调用的语法转换。但是我得到了 forEventClass
的类型错误和 andEventId
setEventHandler
的参数方法:
'NSNumber' is not a subtype of 'AEEventClass'
对于 forEventClass
争论
和:
'NSNumber' is not a subtype of 'AEEventId'
对于 andEventID
争论
我不确定我在这个阶段做错了什么 kInternetEventClass
和 kAEGetURL
是由 apple 定义的常量......当然我不需要转换他们的 NSNumber
输入各自所需的类型?如果我是,我不知道该怎么做。
请您参考如下方法:
据我从文档中可以看出,类采用常量并将其转换为正确的类型:
func applicationWillFinishLaunching(aNotification: NSNotification?) {
var appleEventManager:NSAppleEventManager = NSAppleEventManager.sharedAppleEventManager()
appleEventManager.setEventHandler(self, andSelector: "handleGetURLEvent:withReplyEvent:", forEventClass: AEEventClass(kInternetEventClass), andEventID: AEEventID(kAEGetURL))
}