如何在应用意图中使用编译条件(iOS 16)

huangapple go评论81阅读模式
英文:

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:

如何在应用意图中使用编译条件(iOS 16)

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:

如何在应用意图中使用编译条件(iOS 16)

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.

huangapple
  • 本文由 发表于 2023年6月5日 22:13:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/76407331.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定