Detailed Answer
Each project you create has a module
with the same name as the project. So there are two SwifUI modules here:
- The actual
SwiftUI
- 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