英文:
Provide example parameter in a Siri Tip View
问题
我正在实现iOS 16应用程序快捷方式。我正在使用 SiriTipView
来向用户展示可能性。
我的 AddItem
意图具有一个参数,用于将 Item
添加到哪个 Box
实体中。快捷方式将提示用户输入此参数。
@available(iOS 16.0, *)
struct MyShortcuts: AppShortcutsProvider {
@AppShortcutsBuilder static var appShortcuts: [AppShortcut] {
AppShortcut(intent: AddItem(), phrases: [
"将新项目添加到 \(\.$box) 中的 \(.applicationName)",
"在 \(.applicationName) 中添加新项目",
],
shortTitle: "添加新项目",
systemImageName: "pills"
)
}
}
我想要向 SiriTipView
添加一个示例的 Box
(从用户的现有数据中获取,就像我在消除歧义时所做的那样)。
目前,提示视图未填充实体占位符:
"将新项目添加到 ${box} 中的 <My App Name>"
提示视图定义如下:
SiriTipView(intent: AddItem())
我意识到我可以只更改顶层短语,让它使用没有参数的短语,但我认为向用户展示他们可以提及盒子参数会更有用。
我尝试使用如下方式初始化提示视图的 IntentParameter
:
SiriTipView(intent: AddItem(box: IntentParameter<BoxAppEntity>()))
但我无法弄清楚如何为其提供一个示例的盒子实体。我想我需要知道如何使用具体实体初始化 IntentParameter
。
英文:
I am implementing iOS16 App Shortcuts. I am using SiriTipView
to show users the possibilities.
My AddItem
intent has a parameter for which Box
entity to add the Item
to. The shortcut will prompt for this.
@available(iOS 16.0, *)
struct MyShortcuts: AppShortcutsProvider {
@AppShortcutsBuilder static var appShortcuts: [AppShortcut] {
AppShortcut(intent: AddItem(), phrases: [
"Add new item to \(\.$box) in \(.applicationName)",
"Add new item in \(.applicationName)",
],
shortTitle: "Add New Item",
systemImageName: "pills"
)
}
}
I would like to add an example Box
(from the user's existing data, like I do for the disambiguation) to the SiriTipView
.
Currently, the tip view does not fill in the entity placeholder:
"Add new item to ${box} in <My App Name>"
The tip view is defined like this.
SiriTipView(intent: AddItem())
I realise I could just change the topmost phrase and have it use the phrase without the parameter, but I think it would be useful to the user to see that they can speak the box parameter.
I tried initialising the tip view with an IntentParameter
like this:
SiriTipView(intent: AddItem(box: IntentParameter<BoxAppEntity>))
But I could not figure out how to just give it an example box entity. I guess I need to know how to initialise an IntentParameter with a concrete entity.
答案1
得分: 4
M Y的答案指导我朝着正确的方向前进。
我发现,在许多IntentParameter
的初始化器中,我的具体实体的名称被忽略了,或者在编译时出现了错误。
最后,我不得不这样设置wrappedValue
。
func getIntentParameter() -> IntentParameter<BoxAppEntity?> {
let parameter = IntentParameter<BoxAppEntity?>(title: "Example Parameter")
parameter.wrappedValue = BoxAppEntity(id: "boxID", displayString: "Box Name")
return parameter
}
然后我在SiriTipView
中使用了这个:
SiriTipView(intent: AddItem(box: getIntentParameter()))
在getIntentParameter
方法中,我可以根据一些任意用户数据简单地设置displayString
。
英文:
The answer by M Y led me in the right direction.
I found that with many of the IntentParameter
initialisers, the name of my concrete entity was being ignored, or there were compile time errors.
In the end, I had to set the wrappedValue
like this.
func getIntentParameter() -> IntentParameter<BoxAppEntity?> {
let parameter = IntentParameter<BoxAppEntity?>(title: "Example Parameter")
parameter.wrappedValue = BoxAppEntity(id: "boxID", displayString: "Box Name")
return parameter
}
Then I used this in the SiriTipView
:
SiriTipView(intent: AddItem(box: getIntentParameter()))
In the getIntentParameter
method I can simply set the displayString
based on some arbitrary user data.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论