导入 UIKit 并使用图像作为 AppEnum 的 caseDisplayRepresentations 时出现 AppIntents 问题

ale*_*ner 3 ios swift sirishortcuts appintents

I\xe2\x80\x99m 创建一个简单的 AppIntents 快捷方式,需要 UIKit 在我的应用程序中打开 URL。不过,一旦我将 添加import UIKit到我的文件中,Xcode 就会在 处显示以下警告caseDisplayRepresentations,因为我使用图像来显示表示。无需导入 UIKit(或无需 DisplayRepresentations 的图像),一切正常!有什么想法如何解决这个问题吗?

\n
\n

期望直接调用Image初始化程序,但得到Swift.Optional<AppIntents.DisplayRepresentation.Image>

\n
\n

Xcode 警告屏幕截图

\n

代码

\n
import AppIntents\nimport UIKit\n\nenum AppView: Int, AppEnum {\n    case tab1\n    case tab2\n    \n    static var typeDisplayRepresentation = TypeDisplayRepresentation(name: "View")\n    \n    static var caseDisplayRepresentations: [AppView : DisplayRepresentation] = [\n        .tab1: DisplayRepresentation(title: "Tab 1",\n                                     image: .init(systemName: "1.circle")),\n        .tab2: DisplayRepresentation(title: "Tab 2",\n                                     image: .init(systemName: "2.circle"))\n    ]\n}\n\n// MARK: - Intent\nstruct OpenAppIntent: AppIntent {\n    static let title: LocalizedStringResource = "Open App"\n    static let description: IntentDescription = "Opens the app in the given view."\n    \n    // Launches app when action is triggered\n    static let openAppWhenRun: Bool = true\n    \n    // App View Parameter\n    @Parameter(title: "View",\n               description: "The view inside the app.",\n               default: .tab1,\n               requestValueDialog: "Where would you like to navigate to?")\n    var view: AppView\n    \n    // Shortcuts Action Text\n    static var parameterSummary: some ParameterSummary {\n        Summary("Open \\(\\.$view)")\n    }\n    \n    \n    @MainActor\n    func perform() async throws -> some IntentResult {\n        if let url = URL(string: "myapp://\\(view)") {\n            await UIApplication.shared.open(url)\n        }\n        \n        return .result()\n    }\n}\n
Run Code Online (Sandbox Code Playgroud)\n

小智 5

正如我所看到的,当导入 UIKit 时,它将 Image 默认初始值设定项从返回 Image 更改为Optional<Image>。

请重点关注快速帮助。 前 后

您可以通过强制解开图像来解决此问题(不推荐)

.init(systemName: "1.circle")!
Run Code Online (Sandbox Code Playgroud)

或者通过帮助 Xcode 指定您选择的初始值设定项。

.init(systemName: "1.circle", isTemplate: false)
Run Code Online (Sandbox Code Playgroud)