Xcode 11 Beta 3, build error "Unknown attribute 'State'", "Use of undeclared type 'View'" etc
Asked Answered
S

6

8

I build an app with the 2nd beta of Xcode 11, using SwiftUI, and everything worked completely fine, but now, with the 3rd beta, the app isn't even build. I get errors like "Use of undeclared type 'View'", "Unknown attribute 'State'" etc. What can I do? Is the problem in my code or is it just a bug?

The problem is when I'm trying to build the app to run on my Mac (with UIKit for Mac)

@State var score = 0

The error is "Unknown attribute 'State'"

struct ContentView : View {
    /*...*/
}

The error is "Use of undeclared type 'View'"

Supralapsarian answered 10/7, 2019 at 9:4 Comment(2)
Have you imported the SwiftUI framework?Niemeyer
@SohilR.Memon yes, otherwise it won't work on iOS too.Supralapsarian
L
5

This is from the Xcode 11 Beta 3 Release Notes.

Known Issues

  • Xcode 11 doesn’t support working with SwiftUI in an iPad app brought to macOS. (41416222, 51201699)

I downloaded the Xcode 11 Beta 3 myself just to confirm the errors you mentioned, and I had the same issues when trying to build a SwiftUI project for Mac.

I would suggest going back to using Xcode Beta 2 if possible. Unfortunately, Apple doesn't provide download links to previous betas, so hopefully you have a Time Machine backup or Beta 2 still installed on your computer. Otherwise, you might be out of luck until the next beta is released.

Linseed answered 10/7, 2019 at 10:9 Comment(0)
A
13

Got same issue on Xcode 11.2. Found out that the reason was that somewhere in same module I declared protocol State {}. This breaks compiler without any reasonable errors.

Antelope answered 27/11, 2019 at 15:48 Comment(2)
In that case you can simply do @SwiftUI.StateKibe
This is exactly what I did too but struct State. Thanks!Kadner
L
5

This is from the Xcode 11 Beta 3 Release Notes.

Known Issues

  • Xcode 11 doesn’t support working with SwiftUI in an iPad app brought to macOS. (41416222, 51201699)

I downloaded the Xcode 11 Beta 3 myself just to confirm the errors you mentioned, and I had the same issues when trying to build a SwiftUI project for Mac.

I would suggest going back to using Xcode Beta 2 if possible. Unfortunately, Apple doesn't provide download links to previous betas, so hopefully you have a Time Machine backup or Beta 2 still installed on your computer. Otherwise, you might be out of luck until the next beta is released.

Linseed answered 10/7, 2019 at 10:9 Comment(0)
P
4

I was getting the same "Unknown attribute 'State'" issue in Beta 5. But I was doing "@state" and not "@State". Case matters. It's hard to notice because both ways are shown as purple.

Philine answered 11/8, 2019 at 23:45 Comment(1)
This was me. Goodness I was so careful with capitalization tooPustulant
T
2

If you are doing a Kotlin multi-platform project, check if your shared module has a component named "State".

Thiazole answered 11/5, 2021 at 18:59 Comment(2)
Isn't Kotlin specifically for Android?Ford
@Ford Fortunately no. You can run it on JVM, JS, and Native. Although you may wanna check if JS and Native are "production-ready" enough for your usecase.Thiazole
M
2

I got the same error while trying to compile a Swift project created with Xcode 13 on Xcode 11. The problem was that the newer version uses the SDK 12.1 while the older one uses the SDK 10.15. To let the project compile on the older Xcode/SDK I just performed these steps:

  • remove the @main attribute from the swift source code

  • add Cocoa.framework in the section Frameworks, Libraries, and Embedded Content

  • add a file named main.m to the project (no bridge header) with this content:

#include <Cocoa/Cocoa.h>

int main(int argc, const char * argv[]) {

    return NSApplicationMain(argc, argv);
}
Munich answered 22/2, 2022 at 10:49 Comment(0)
R
1

Instead @main, please replace @UIApplicationMain. I've attached screenshots. https://prnt.sc/vimvwn

Realistic answered 13/11, 2020 at 13:53 Comment(1)
This is also true to macOS Apps. If you run into the error in XCode 11 Unknown attribute 'Main' then replace @Main with @NSApplicationMain. Different issue but same solution.Tardy

© 2022 - 2024 — McMap. All rights reserved.