【发布时间】:2014-06-06 10:58:51
【问题描述】:
我一直在尝试在 Swift 中复制 Objective-C 中执行此操作的常用方法,用于我正在玩的一个新的 swift 应用程序。
如何在 Objective-C 中执行此操作已有详细记录,您获取共享的 Apple 事件管理器并调用 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 调用的语法转换。但是对于 setEventHandler 方法的 forEventClass 和 andEventId 参数,我都遇到了类型错误:
'NSNumber' is not a subtype of 'AEEventClass' 用于 forEventClass 参数
和:
'NSNumber' is not a subtype of 'AEEventId' 用于 andEventID 参数
我不确定我在这个阶段做错了什么,因为kInternetEventClass 和kAEGetURL 都是苹果定义的常量...当然我不需要将它们的NSNumber 类型转换为它们各自的类型需要的类型?如果我是,我不知道怎么做。
【问题讨论】: