Package struct embedded in different files

I'm using Swift with version 6.1.2 and Xcode 16.4
My files are in a Swift package.

Here are two cases: one that doesn't work and one that does.

Case that doesn't work

File: MainView.swift

public struct MainView: View {
  // view code here, calling SubView 
  //    => compiler error: ❌ Cannot find 'SubView' in scope
}

File: MainView.SubView.swift

extension MainView {
  public struct SubView: View {
  }
}

If I do this, the compiler says SubView is not visible from MainView.

Case that works

With MainView and SubView in the same file

File: MainView.swift

public struct MainView: View {
  // view code here, calling SubView βœ…
}

extension MainView {
  public struct SubView: View {
  }
}

I don’t understand why the compiler can’t see SubView if it's not in the same file as MainView.

Is this a limitation of the compiler?

is this extension MainView { is the same package as MainView ?

if you make it public is it visible? I think public is not needed since its part of same package.

is this extension MainView { is the same package as MainView ?

Yes

MyPackage/
β”œβ”€β”€ Package.swift
└── Sources/
    └── MyModule/
        β”œβ”€β”€ MainView.swift
        └── MainView.SubView.swift

if you make it public is it visible? I think public is not needed since its part of same package.

I agree, I made it public hoping that MainView would see SubView.

I found it!
On other projects, I don't have any issues with embedded structs.
If I open the package directly from the Package.swift file, I have no issues.
If I open the test project, which opens the package locally, that's where I have the problem.
To resolve the problem, I deleted xshareddata and xuserdata in project.xcworspace.
The problem was not with the package but with Xcode.

2 Likes