我正在尝试将代码迁移到Swift 3,并遇到有关尝试处理3D Touch快捷方式的错误。
我收到以下错误
使用3D Touch快捷键执行搜索-模棱两可的参考
成员“下标”
对于以下行
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: Any]?) -> Bool {
if let shortcutItem = launchOptions?[UIApplicationLaunchOptionsShortcutItemKey] as? UIApplicationShortcutItem {
handleShortcutItem(shortcutItem)
}
}
我不确定为什么要得到这个,其他人知道吗?
这是我处理快捷方式的方式
enum ShortcutItemType: String {
case First
case Second
case Third
init?(shortcutItem: UIApplicationShortcutItem) {
guard let last = shortcutItem.type.components(separatedBy: ".").last else { return nil }
self.init(rawValue: last)
}
var type: String {
return Bundle.main.bundleIdentifier! + ".\(self.rawValue)"
}
}
fileprivate func handleShortcutItem(_ shortcutItem: UIApplicationShortcutItem) {
if let rootViewController = window?.rootViewController, let shortcutItemType = ShortcutItemType(shortcutItem: shortcutItem) {
let rootNavController = rootViewController.childViewControllers.first as! UINavigationController
let viewController = rootNavController.childViewControllers[1]
switch shortcutItemType {
case .First:
viewController.performSegue(withIdentifier: "firstSegue", sender: self)
break
case .Second:
viewController.performSegue(withIdentifier: "secondSegue", sender: self)
break
case .Third:
//Segue to view controller from first then perform another segue to a modal view.
viewController.performSegue(withIdentifier: "thirdSegue", sender: self)
break
}
}
}
func application(_ application: UIApplication, performActionFor shortcutItem: UIApplicationShortcutItem, completionHandler: (Bool) -> Void) {
handleShortcutItem(shortcutItem)
}
最佳答案
application(_:didFinishLaunchingWithOptions:)
的方法标头已更改为:
func application(_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey : Any]? = nil)
符号
UIApplicationLaunchOptionsShortcutItemKey
已替换为UIApplicationLaunchOptionsKey.shortcutItem
。这可能是另一个问题,但是
application(_:performActionFor:completionHandler:)
需要具有以下标头:func application(_ application: UIApplication,
performActionFor shortcutItem: UIApplicationShortcutItem,
completionHandler: @escaping (Bool) -> Void)
尝试修复所有问题。
关于ios - 使用UIApplicationShortcutItem时对成员“下标”的引用不明确,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/39421229/