Tof
June 3, 2025, 6:45am
1
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.
Tof
June 3, 2025, 6:56am
3
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.
Tof
June 3, 2025, 9:30am
4
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