5

It seems that WidgetBundle has a maximum number limit, if it exceeds 5, a compile error will be reported: Extra argument in call.

But I have not seen such a description in any document, and no other developers mentioned this issue.

Does anyone have an idea?

@main
struct WidgetsBundle: WidgetBundle {
    @WidgetBundleBuilder
    var body: some Widget {
        Widget1()
        Widget2()
        Widget3()
        Widget4()
        Widget5()
        Widget6() // Extra argument in call
    }
}
qiz
  • 869
  • 1
  • 9
  • 14

2 Answers2

15

I am experiencing same error within WidgetBundle. If you are interested in having more than 5 widgets for the app, there is a workaround:

@main
struct WidgetKitExtension: WidgetBundle {
    @WidgetBundleBuilder
    var body: some Widget {
        Widget1()
        Widget2()
        Widget3()
        Widget4()
        Bundle2().body
    }
}

struct Bundle2: WidgetBundle {
    @WidgetBundleBuilder
    var body: some Widget {
        Widget5()
        Widget6()
        Widget7()
    }
}

Basically, you create second WidgetBundle and use it in your first one. I have verified that this works as of Xcode 12.0.1

Haven't tested submitting the app with more than 5 widgets though.

sabius
  • 704
  • 7
  • 10
  • 1
    It worked! Thank you for your answer. I tried and can even use a `Bundle3` in `Bundle2` to make more than 25 widgets. If the submission is approved, I will reply here. – qiz Oct 09 '20 at 04:43
  • 5
    The submission is approved by Apple. – qiz Oct 14 '20 at 02:55
1

For information, the limit of five widgets is because the WidgetBundleBuilder result builder only had versions of buildBlock for 1-5 entries.

You can see this limit by selecting the WidgetBundleBuilder in your code, right clicking and selecting "Jump to Definition". Scroll down a little and you will see overloads in SwiftUI for buildBlock going as high as five template parameters.

I just noticed that Xcode 14 / iOS 16 has raised this limit to ten entries: there are now buildBlock overloads going as high as ten template parameters.

This is a compile-time limitation. An Xcode 14 build using up to ten widgets in a single WidgetBundleBuilder's body method now compiles and works when run on iOS 15 (I've not tried iOS 14). The workaround would still be required to exceed 10 widgets.

Geoff Hackworth
  • 2,673
  • 1
  • 16
  • 16