我无法在 iOS 15 的 WidgetConfiguration 中添加 systemExtraLarge 系列

Tom*_*scó 5 ios swift widgetkit ipados xcode13

我想在我的应用程序中添加一个超大的小部件作为 iOS 15 支持的系列。

WidgetConfiguration的简化代码如下:

    var body: some WidgetConfiguration {
        IntentConfiguration(
            kind: "Widget",
            intent: SelectProjectIntent.self,
            provider: Provider()
        ) {
            entry in
            ProgressWidgetEntryView(entry: entry)
        }
        .configurationDisplayName("Title")
        .description("Description")
        .supportedFamilies([.systemSmall, .systemMedium, .systemLarge, .systemExtraLarge])
    }
Run Code Online (Sandbox Code Playgroud)

显然我不能只是简单地添加额外的大,因为出现以下错误: “systemExtraLarge”仅在 iOS 15.0 或更高版本的应用程序扩展中可用

但是,按照 XCode 的建议进行快速简单的可用性检查后,我收到了一个错误和几个警告。这是代码:

    var body: some WidgetConfiguration {
        
        if #available(iOSApplicationExtension 15.0, *) {
            
            IntentConfiguration(
                kind: "Widget",
                intent: SelectProjectIntent.self,
                provider: Provider()
            ) {
                entry in
                ProgressWidgetEntryView(entry: entry)
            }
            .configurationDisplayName("Title")
            .description("Description")
            .supportedFamilies([.systemSmall, .systemMedium, .systemLarge, .systemExtraLarge])
            
        } else {
            
            IntentConfiguration(
                kind: "Widget",
                intent: SelectProjectIntent.self,
                provider: Provider()
            ) {
                entry in
                ProgressWidgetEntryView(entry: entry)
            }
            .configurationDisplayName("Title")
            .description("Description")
            .supportedFamilies([.systemSmall, .systemMedium, .systemLarge])
        }
    }
Run Code Online (Sandbox Code Playgroud)

错误是:Function 声明了一个不透明的返回类型,但在其主体中没有 return 语句来从中推断出基础类型

还有两个警告:Result of call to 'supportedFamilies' isused

有人可以向我解释一下为什么我会收到此错误以及如何修复它,以便我可以保留 iOS 14 的小部件并为 iOS 15 添加 systemExtraLarge 吗?

我在 macOS Monterey 版本 12.0 beta (21A5304g) 上使用 XCode 版本 13.0 beta 5

提前致谢。

Ada*_*dam 15

当您创建视图时,它被var body标记为@ViewBuilder. 这允许您提供多个子视图并使用条件(了解更多)。不幸的是,这不适用于 a 的主体,Widget因为不存在 a 之类的东西WidgetConfigurationBuilder,因此您只能从 Widget\xe2\x80\x99s 返回单个 WidgetConfigurationbody

\n

要解决您的问题,请if-else从小部件中取出body. 一种方法是将其移动到属性,如下所示:

\n
struct MyWidget: Widget {\n    \n    private let supportedFamilies:[WidgetFamily] = {\n        if #available(iOSApplicationExtension 15.0, *) {\n            return [.systemSmall, .systemMedium, .systemLarge, .systemExtraLarge]\n        } else {\n            return [.systemSmall, .systemMedium, .systemLarge]\n        }\n    }()\n    \n    var body: some WidgetConfiguration {\n        IntentConfiguration(\n            kind: "Widget",\n            intent: SelectProjectIntent.self,\n            provider: Provider()\n        ) {\n            entry in\n            ProgressWidgetEntryView(entry: entry)\n        }\n        .configurationDisplayName("Title")\n        .description("Description")\n        .supportedFamilies(supportedFamilies)\n    }\n}\n
Run Code Online (Sandbox Code Playgroud)\n