无法在Swift 2.3中编译Siri扩展

3
我想在Swift 2.3版本的基础上,将Siri/Intent扩展集成到我的App中。我正在使用Xcode 8 beta 6构建。但是该应用程序甚至无法编译,并输出错误消息,如下所示:“Type 'IntentHandler' does not conform to protocol 'INSendMessageIntentHandling'。” “Protocol requires function 'handle(sendMessage:completion:)' with type '(sendMessage: INSendMessageIntent, completion: (INSendMessageIntentResponse) -> Void) -> Void'”。而且,“Objective-C method 'handleWithSendMessage:completion:' provided by method 'handle(sendMessage:completion:)' does not match the requirement's selector ('handleSendMessage:completion:')”。即使是苹果公司提供的样例应用程序UnicornChat也有同样的问题。我已经将SWIFT_VERSION标志更改为“Yes”,以支持Swift 2.3。以下是代码示例:
func handle(sendMessage intent: INSendMessageIntent, completion: (INSendMessageIntentResponse) -> Void) {
        let userActivity = NSUserActivity(activityType: NSStringFromClass(INSendMessageIntent.self))
        let response = INSendMessageIntentResponse(code: .Success, userActivity: userActivity)
        completion(response)
    }

出现了上述错误,操作失败。

有人知道如何在Swift 2.3中使Siri正常工作吗?


你实现了 "func handle(sendMessage intent: ...)" 吗?如果是,请展示给我们看。 - Jeshua Lacock
@JeshuaLacock 请检查修改。 - Arnav Patra
3个回答

6

所以,这很有帮助。

https://forums.developer.apple.com/message/174455#174455

我们需要使用Swift方法来明确定义@objc方法的签名。如下所示:

@objc (handleSendMessage:completion:)
func handle(sendMessage intent: INSendMessageIntent, completion: (INSendMessageIntentResponse) -> Void) {
 //..
}

1
显然,Xcode 8 beta 6已更新为Swift 3,并且回调需要在方法中添加@escaping。
func handle(sendMessage intent: INSendMessageIntent, completion: @escaping (INSendMessageIntentResponse) -> Swift.Void) {  
  //..  
}

如果Siri委托方法已经更新到Swift 3(似乎是这种情况),那么恐怕您也必须使用Swift 3,或者使用旧版本的SDK。

OP正在使用Swift 2.3。 - kometen
我知道这一点 - 但看起来委托方法已经更新为Swift 3。 - Jeshua Lacock

0

在使用Swift 2.3编译代码时,需要为Siri和SiriUI目标设置Use Legacy Swift Language VersionYES

此外,在创建IntentHandlerIntentViewController时,方法可能已经是Swift 3(就像我遇到的情况)。因此,将类转换为Swift 2.3,并使用@objc方法签名以及委托。

对于IntentHandler

@objc (handleSendMessage:completion:)
func handle(sendMessage intent: INSendMessageIntent, completion: (INSendMessageIntentResponse) -> Void) {
}

@objc (handleSearchForMessages:completion:)
func handle(searchForMessages intent: INSearchForMessagesIntent, completion: (INSearchForMessagesIntentResponse) -> Void) {
}

@objc (handleSetMessageAttribute:completion:)
func handle(setMessageAttribute intent: INSetMessageAttributeIntent, completion: (INSetMessageAttributeIntentResponse) -> Void) {
}

对于IntentViewController

@objc (configureWithInteraction:context:completion:)
func configureWithInteraction(interaction: INInteraction!, context: INUIHostedViewContext, completion: ((CGSize) -> Void)!) {
}

希望这有所帮助。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接