Xcode Ignoring import

Viewed 8765

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

3 Answers

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

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

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

Related