英文:
How to use compiler conditionals in an app intent (iOS 16)
问题
以下是您要翻译的内容:
我正在尝试构建一个 AppIntent(iOS 16),其中用户可以在快捷方式应用中输入数据的参数。
代码看起来像这样:
import Foundation
import AppIntents
struct MyIntent: AppIntent {
static var title = LocalizedStringResource("Intent title")
@Parameter(title: "user value", default: "not set")
var myParameterVariableName: String
static var parameterSummary: some ParameterSummary {
Summary("Your Value: \($myParameterVariableName)")
}
@MainActor func perform() async throws -> some IntentResult {
print("action")
return .result()
}
}
当我尝试使用该意图配置快捷方式时,在快捷方式应用中看起来像预期的那样:
[![enter image description here][1]][1]
然而,参数的默认值应该根据活动的构建配置而不同。所以我尝试了这个:
#if DEBUG
@Parameter(title: "user value", default: "debug")
var myParameterVariableName: String
#else
@Parameter(title: "user value", default: "not set")
var myParameterVariableName: String
#endif
但这会导致在快捷方式应用中显示变量名称:
[![enter image description here][2]][2]
无论我选择哪个构建配置来构建,它都不会显示“debug”或“not set”作为默认值。我可以点击它并输入数据,但默认值不会显示。
有什么想法吗?是否有其他方法来替代变量而不是使用预处理器宏?
[1]: https://i.stack.imgur.com/p0woU.png
[2]: https://i.stack.imgur.com/BfnNW.png
英文:
I am trying to build an AppIntent (iOS 16) with a parameter where the user can enter data in the shortcuts app.
The code looks like that:
import Foundation
import AppIntents
struct MyIntent: AppIntent {
static var title = LocalizedStringResource("Intent title")
@Parameter(title: "user value", default: "not set")
var myParameterVariableName: String
static var parameterSummary: some ParameterSummary {
Summary("Your Value: \(\.$myParameterVariableName)")
}
@MainActor func perform() async throws -> some IntentResult {
print("action")
return .result()
}
}
Which looks as expected in the shortcuts app when I try to configure a shortcut with that intent:
However, the parameter's default value should be different based on the active build configuration. So I tried this:
#if DEBUG
@Parameter(title: "user value", default: "debug")
var myParameterVariableName: String
#else
@Parameter(title: "user value", default: "not set")
var myParameterVariableName: String
#endif
But this leads to displaying the variable name in the shortcuts app:
It does not display "debug" or "not set" as the default value no matter which build configuration i choose to build. I can tap on it and enter data, but the default value is not displayed.
Any ideas? Are there other ways to replaces variables that to use preprocessor macros?
答案1
得分: 0
这已经在Xcode 15 beta中修复。编译器条件现在在应用意图中起作用。
英文:
This has been fixed in Xcode 15 beta. Compiler conditionals are working now in App intents.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论