首页 > 解决方案 > 如何检查快捷方式是否已经存在?

问题描述

我已经编写了这个方法来检查给定 id 的快捷方式是否已经存在。

public static func shortcutExists(id: String, completion: @escaping ((Bool) -> Void)) {
        var exists = false
        INVoiceShortcutCenter.shared.getAllVoiceShortcuts { voiceShortcutsFromCenter, error in
            if let voiceShortcutsFromCenter = voiceShortcutsFromCenter {
                for voice in voiceShortcutsFromCenter {
                    if id == voice.shortcut.intent?.identifier {  // What to do here?
                        exists = true
                        break
                    }
                }
            } else {
                ...
            }
            completion(exists)
        }
    }

我为每个意图条目都有一个唯一的 ID。但我不知道如何将该 ID 保存为意图的标识符。标识符似乎相当只读,并且似乎是自动生成的。

请问我怎样才能做到这一点?

标签: iosswift

解决方案


我如何实现这一点是在每个自定义意图中添加一个独特的属性。然后在之后检查意图的属性getAllVoiceShortcuts{}

func checkExist(existShortcuts: [INVoiceShortcut]) -> Bool {
    existShortcuts.first(where: {
        let intent = $0.shortcut.intent
        return intent?.customPropertyId == self.customId
    }) != nil
}

推荐阅读