我一直在尝试在 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争论

我不确定我在这个阶段做错了什么 kInternetEventClasskAEGetURL是由 apple 定义的常量......当然我不需要转换他们的 NSNumber输入各自所需的类型?如果我是,我不知道该怎么做。

请您参考如下方法:

据我从文档中可以看出,类采用常量并将其转换为正确的类型:

func applicationWillFinishLaunching(aNotification: NSNotification?) { 
    var appleEventManager:NSAppleEventManager = NSAppleEventManager.sharedAppleEventManager() 
    appleEventManager.setEventHandler(self, andSelector: "handleGetURLEvent:withReplyEvent:", forEventClass: AEEventClass(kInternetEventClass), andEventID: AEEventID(kAEGetURL)) 
} 


评论关闭
IT虾米网

微信公众号号:IT虾米 (左侧二维码扫一扫)欢迎添加!

ios之两个 UIBezierPaths 交集作为一个 UIBezierPath