Xcode Ignoring import Xcode Ignoring import xcode xcode

Xcode Ignoring import


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.


Hoe 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 SwiftUIimport MyProjectSection // <- 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 SectionMyProject.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