Can't figure out why Toolbar is not conforming to view

ToolbarItem in group is not conforming to view for some reason, here's my code:

var body: some View {
    NavigationStack {
        Form {
            TextField("Company Name", text: $companyName)
            TextField("Role", text: $role)
            TextField("Location", text: $location)
            TextField("Yearly Salary", value: $yearlySalary, format: .currency(code: "USD"))
                .keyboardType(.decimalPad)
        }
        .navigationTitle("Add Application")
        .navigationBarTitleDisplayMode(.large)
        .toolbar {
            ToolbarItem(placement: .navigationBarLeading) {
                Button("back") {
                    dismiss()
                }
            }
            ToolbarItem(placement: .navigationBarTrailing) {
                Button("Save") {
                    let appdata = ApplicationData(
                        companyName: companyName,
                        role: role,
                        location: location,
                        yearlySalary: yearlySalary,
                        dateApplied: dateApplied,
                        notes: notes)
                    // Save the application data
                    dismiss()
                }
            }
        }
    }
}

The error reads: Static method 'buildExpression' requires that 'ToolbarItem<(), Button<Text>>' conform to 'View'

I'm trying to change it up but I can't get past this error for some reason

Welcome to the forum.

I tested your code (after completing missing parts and removing reference to unknown structure) and it works OK, both with Xcode 16.0ß2 and Xcode 15.0.1.

  • Are you sure you have declared var as State vars ?
  • Have you declared dismiss ?
  • Aren't you missing a closing curly bracket somewhere ?
  • Where exactly do you get the error ?

Please show the exact and complete code, as well as your configuration (Xcode version, system version…)

Here is the complete code I tested:

struct ContentView: View {
    @State var companyName: String = ""
    @State var role: String = ""
    @State var location: String = ""
    @State var yearlySalary: Double = 0
    @Environment(\.dismiss) var dismiss

    var body: some View {
        NavigationStack {
            Form {
                TextField("Company Name", text: $companyName)
                TextField("Role", text: $role)
                TextField("Location", text: $location)
                TextField("Yearly Salary", value: $yearlySalary, format: .currency(code: "USD"))
                    .keyboardType(.decimalPad)
            }
            .navigationTitle("Add Application")
            .navigationBarTitleDisplayMode(.large)
            .toolbar {
                ToolbarItem(placement: .navigationBarLeading) {
                    Button("back") {
                        dismiss()
                    }
                }
                ToolbarItem(placement: .navigationBarTrailing) {
                    Button("Save") {
//                        let appdata = ApplicationData(
//                            companyName: companyName,
//                            role: role,
//                            location: location,
//                            yearlySalary: yearlySalary,
//                            dateApplied: dateApplied,
//                            notes: notes)
//                        // Save the application data
                        dismiss()
                    }
                }
            }
        }
    }
}

Sorry for late response,

here is the full struct

import SwiftUI

struct addApplicationSheet: View {

@Environment(\.dismiss) var dismiss

@State var companyName: String = ""
@State var role: String = ""
@State var location: String = ""
@State var yearlySalary: Double = 0.00
@State var dateApplied: Date = .now
@State var notes: String = ""
``

var body: some View {
    NavigationStack {
        Form {
            TextField("Company Name", text: $companyName)
            TextField("Role", text: $role)
            TextField("Location", text: $location)
            TextField("Yearly Salary", value: $yearlySalary, format: .currency(code: "USD"))
                .keyboardType(.decimalPad)
        }
        .navigationTitle("Add Application")
        .navigationBarTitleDisplayMode(.large)
        .toolbar {
            ToolbarItem(placement: .navigationBarLeading) {
                Button("back") {
                    dismiss()
                }
            }
            ToolbarItem(placement: .navigationBarTrailing) {
                Button("Save") {
                    let appdata = ApplicationData(
                        companyName: companyName,
                        role: role,
                        location: location,
                        yearlySalary: yearlySalary,
                        dateApplied: dateApplied,
                        notes: notes)
                    // Save the application data
                    dismiss()
                }
            }
        }
    }
}

}

#Preview {

addApplicationSheet()

}

I messed up some of the inline code but that's everything

I'm running Xcode Version 15.3 on Sonoma 14.4.1

Can't figure out why Toolbar is not conforming to view
 
 
Q