Xcode Ignoring import
Asked Answered
P

3

43

I have just installed Xcode 11 and when I try to create new fresh project with the SwiftUI check mark selected it returns an error.

Not able to build and run successfully.

File 'ContentView.swift' is part of module 'SwiftUI'; ignoring import

ContentView.swift

enter image description here

Use of undeclared type 'View'

SceneDelegate.swift

enter image description here

Use of unresolved identifier 'UIHostingController'

I have tried removing all derived data and also set command-line tools to 11

Papacy answered 8/6, 2019 at 11:10 Comment(2)
Are you macOS 10.15 ?Bastardize
No using version 10.14.5Papacy
A
154

Your project is named SwiftUI - please try using a different name.

Acquisitive answered 8/6, 2019 at 11:17 Comment(4)
Yes let me try with different namePapacy
It's working now i forgot that we can't use class name :)Papacy
@Papacy An app is still a Swift module - and it gets resolved as a dependency before the actual framework. So import SwiftUI meant import the app module, hence the import being ignored by XcodeAcquisitive
I had the same issue when I created a MacOs/SwiftUI project named CoreData!Pierre
J
2

Detailed Answer

Each project you create has a module with the same name as the project. So there are two SwifUI modules here:

  1. The actual SwiftUI
  2. The project itself

Xcode always takes the nearest definition as the default. So your SwiftUI is closer than the system's SwiftUI. But you are in the project's module already! So Xcode ignores the import.

A very common mistake is to name the project same as one of the using frameworks! (e.g. CoreData, SwiftUI, SceneKit, Metal)

Solution

As Matteo mentioned in his answer, Don't name your project same with another module. Change it to anything else.


Note that It could appear as an error too. For example, if you name your project CoreData and using SwiftUI, the error appears as Circular dependency error:

Circular dependency between modules 'CoreData' and 'SwiftUI'

Because Xcode gets confused about modules and can not detect what the real issue is.


How can we access our module's classes instead of the system's module?

Imagine you have a class named Section in a custom framework called MyProject and you imported it alongside the SwiftUI.

import SwiftUI
import MyProject

Section // <- This could be either SwiftUI's section or MyProject's Section

To make it clear for the compiler (and anyone else), you should call the module before the class name:

SwiftUI.Section // <- This returns the SwiftUI's Section

MyProject.Section // <- This returns the MyProject's Section
Joke answered 10/8, 2020 at 4:41 Comment(0)
W
-1

Try with different project name. With SwiftUI, it will always show compilation error. Just change the name and enjoy coding with SwiftUI

Whiffen answered 26/7, 2019 at 18:2 Comment(1)
You basically retyped Matteo's answer. Don't duplicate answer without meaningful purposes.Helldiver

© 2022 - 2024 — McMap. All rights reserved.