如何在快捷指令创建器中单独设置AppIntents的输出标签?
解决AppIntents自定义Intent输出标签与动作标题分离的问题
要实现保留Intent的title用于动作列表展示,同时单独设置输出结果的显示标签,你可以在返回IntentResult时,通过DisplayRepresentation自定义输出的显示内容,无需修改static var title字段。
修改后的代码示例
struct MyIntent: AppIntent { static var title: LocalizedStringResource = "This is the title" static var description = IntentDescription("This is the description.") static var parameterSummary: some ParameterSummary { Summary("This is the parameter summary") } func perform() async throws -> some IntentResult { // 通过DisplayRepresentation指定输出标签 return .result(value: true) { DisplayRepresentation(title: "自定义输出标签") } } }
原理说明
static var title依然负责动作列表中该Intent的展示名称,保持原有功能不变。- 在
perform方法返回结果时,传入的DisplayRepresentation会专门定义该Intent输出结果在快捷指令后续动作中的显示标签,对应你示例里的If (xxx)部分。
如果你的Intent返回的是自定义数据类型,还可以让该类型遵循CustomIntentResultDisplayable协议,统一定义其在快捷指令中的显示样式:
struct CustomData: Codable, CustomIntentResultDisplayable { var value: String var displayRepresentation: DisplayRepresentation { DisplayRepresentation(title: "自定义数据标签") } }
这样无论在哪个Intent中返回该类型,输出标签都会统一使用你定义的内容,无需重复设置。
内容的提问来源于stack exchange,提问作者JBYoshi
相关产品推荐
相关产品推荐

