无法在 Swift 3/iOS 10 中触发 continueUserActivity [英] cannot trigger continueUserActivity in Swift 3 / iOS 10

查看:59
本文介绍了无法在 Swift 3/iOS 10 中触发 continueUserActivity的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

注意:我已经解决了大部分问题 - 请参阅最后的更新.还是有些迷茫.

Note: I figured most of this out - see the update at the end. Still some confusion.

我正在尝试在 Swift 3 下的 Xc8b6 中实现 NSUserActivity 处理,并且在处理程序协议方法的方法签名方面遇到问题.

I'm trying to implement NSUserActivity handling in Xc8b6 under Swift 3 and having trouble with the method signature for the handler protocol method.

在当前文档中,该方法被称为:

In the current doc, the method is said to be:

func application(_ application: UIApplication, 
                 continue userActivity: NSUserActivity, 
       restorationHandler: @escaping ([Any]?) -> Void) -> Bool

这与我在 swift 标头界面中看到的仅略有不同.在那里,repositoryHandler 中的返回类型是 Swift.Void.

This differs only slightly from what I see in the swift header interface. There, the return type in the restorationHandler is Swift.Void.

当我尝试这个时,我得到的错误是:

When I try this, the error I get is:

Objective-C 方法 'application:continue:restorationHandler:' 提供通过方法 'application(_:continue:restorationHandler:)' 不匹配需求选择器('application:continueUserActivity:restorationHandler:')

Objective-C method 'application:continue:restorationHandler:' provided by method 'application(_:continue:restorationHandler:)' does not match the requirement's selector ('application:continueUserActivity:restorationHandler:')

我意识到现在编译器警告不是很好,但我认为这意味着它找到了 Obj-C 方法签名,但不知何故我的参数并不完全匹配.

I realize the compiler warnings aren't great right now, but I take this to mean it's found the Obj-C method signature for this, but that somehow my arguments don't quite match.

我不知道还有什么地方可以用这个.它与那里的内容相匹配 - 但 something 是错误的.随机猜测和检查还没有帮助我.

I'm not sure where else to go with this. It matches what's there - but something is wrong. Random guessing and checking has not helped me yet.

有趣的是,自动完成给了我一个不同的方法签名:

Interestingly, autocomplete gives me a different method signature:

public func application(_ application: UIApplication, continue userActivity: NSUserActivity, restorationHandler: ([Any]?) -> Void) -> Bool

关于这个的好处是它可以编译.不太好的事情是当我从 Spotlight 中选择一个索引项目并点击它以启动应用程序时它不会被触发.

Nice thing about this one is that it compiles. Less nice thing is that it doesn't get triggered when I select an indexed item from Spotlight and tap it to launch the app.

显然我的 NSUserAction 正确识别了应用程序并启动了它.有什么我可能会在这里遗漏的地方,它不会触发restoreHandler?请注意,这是从在 iOS 9 下运行良好的项目转换而来的.

Clearly my NSUserAction correctly identified the app and launched it. Is there something I could be missing here where it wouldn't fire off restorationHandler? Note this is converted from a project that ran fine under iOS 9.

我还在 WWDC 视频搜索新功能"中看到了另一个版本的协议方法签名.我试过了,它编译了,但也没有触发.

I've also seen yet another version of the protocol method signature in the WWDC video on What's New With Search. I tried that, and it compiled but didn't trigger either.

简单来说,这是我在 Swift 3 转换工具完成后的签名.它已编译,但在从聚光灯下启动时没有被触发:

Just to round things out, this is the signature I had after the Swift 3 conversion tool completed. It compiled, but didn't get triggered when launching from spotlight:

func application(application: UIApplication, continueUserActivity userActivity: NSUserActivity, restorationHandler: ([AnyObject]) -> Void) -> Bool

----更新-----

----Update-----

好的.所以上面提到的第一个方法签名是正确的.问题是我在 AppDelegate 的扩展中定义了它.这在 Swift 2.x 中运行良好.

Ok. So the first method signature mentioned above IS the correct one. The problem is that I was defining it in an extension to the AppDelegate. This worked fine in Swift 2.x.

我已经把它向上移动了,一切都很好.但是我想更好地理解为什么在从扩展中实现协议方法时会出现这些编译错误.它不再符合协议吗?如果我尝试将它添加到扩展程序中,我会收到一条错误消息,指出它是重复的,所以我认为不是这样.

I've moved it up, and everything is fine. But I would like to get a better understanding on why I get these compile errors when implementing a protocol method from an extension. Does it no longer carry thru the protocol conformance? If I try to add it onto the extension, I get an error indicating that it's a duplicate, so I don't think that's quite it.

推荐答案

最初的问题是关于为什么 continueUserActivity 没有被调用(以及正确的签名是什么)所以我会在这里回答这个问题,增加一些关于发生了什么的澄清错误的.

The original question was about why continueUserActivity wasn't getting called (and what the correct signature was) so I'll answer that here adding some clarity on what went wrong.

正确的签名是我放在帖子顶部的:

The correct signature is what I put at the top of my post:

func application(_ application: UIApplication, 
                 continue userActivity: NSUserActivity, 
       restorationHandler: @escaping ([Any]?) -> Void) -> Bool

请记住,此签名与 Xc8b6 中自动完成提供的签名不同,因此我怀疑其他人可能会遇到此问题并从此响应中受益.

Keep in mind this signature is DIFFERENT from what autocomplete provides in Xc8b6, so I suspect someone else might hit this and benefit from this response.

我最初的问题是它没有编译,因为我是从扩展程序中进行编译的.为清楚起见:

My original issue is that it wasn't compiling because I was doing it from within an extension. For clarity:

extension AppDelegate {
  func application(_ application: UIApplication,
                   continue userActivity: NSUserActivity,
                   restorationHandler: @escaping ([Any]?) -> Void) -> Bool {
    return true
  }
}

这适用于任何较旧的 iOS 9/Swift 2.x 项目(使用较旧的 continueUserActivity 签名).我仍然不确定为什么,但它不会在 Xc8/Swift 3 的扩展中编译.

This worked in any older iOS 9 / Swift 2.x project (with the older continueUserActivity signature). I'm still not positive why, but it won't compile in an extension in Xc8 / Swift 3.

我把它移到了类作用域,现在工作正常.

I moved it to the class scope and it works fine now.

这篇关于无法在 Swift 3/iOS 10 中触发 continueUserActivity的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆