Separating Structs into their own files

Blockquote

struct ContentView: View {
    @State private var path = NavigationPath()
    
    var body: some View {
        NavigationStack(path: $path) {
            Button {
                path.append("SecondView")
            } label: {
                Text("This is the first view")
                
            }
            .navigationDestination(for: String.self) { view in
                switch view {
                case "SecondView":
                    SecondView(path: $path)
                case "ThirdView":
                    ThirdView()
                default:
                    Text("Unknown")
                }
            }
        }
    }
}

struct SecondView: View {
    @Binding var path: NavigationPath
    var body: some View {
        Button {
            path.append("ThirdView")
        } label: {
            Text("This is the second view")
            
        }
    }
}

struct ThirdView: View {
    var body: some View {
        Text("This is the third view")
    }
}

struct SecondView_Previews: PreviewProvider {
    static var previews: some View {
        NavigationStack {
            SecondView(path: .constant(NavigationPath()))
        }
    }
}

When I try to place struct SecondView into its own file and comment out struct2 in ContentView I get errors

Why am I getting these errors?
Please help.

Todd

Hey Todd!

You need to import SwiftUI at the top of the file :+1:

The View protocol is part of SwiftUI, not part of Foundation

thank you so much

When you use different frameworks, you’ll have to import them at the top! Doesn’t matter if it’s SwiftUI, UIKit, or others