'SwiftUI Widget: 'main' attribute can only apply to one type in a module

I've created a Widget and I'm adding @main on top of the declaration.

However, when I switch to the Widget extension target and run it, I get the error 'main' attribute can only apply to one type in a module because I also have a @main in my main app file (the entry point). I need to, depending on whether I want to run the app or the widget target, remove the @main from the target that I don't want to run, run it and later on add it.

Is there something I'm doing wrong? I have tried to look this up and I haven't found any mention of it.

Thanks in advance!



Solution 1:[1]

You can only have one entry-point per target. You might have one of the two files (defining @main) included in the wrong target.

Open up both files in XCode, then open Inspectors and check if the Widget-related file target-membership is the WidgetExtension (only) and the App related file in the Apps target.

This might resolve your issue.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Dominik Seemayr