closure containing a declaration cannot be used with result builder 'ViewBuilder'

Trying to finalize my app but I'm getting an error message (closure containing a declaration cannot be used with result builder 'ViewBuilder') on my func addEntry [highlighted]. Meed to know what am I missing here so I can preview and continue development. I'm totally beginner here and build this code searching on several sites. Any help more than welcome -- code below--

import SwiftUI import MessageUI import Foundation

struct ContentView: View {       struct InventoryEntry: Identifiable {     var id = UUID()     let parkingSpot: String     let trailerNumber: String     let tagID: String     let spotType: String     let date: String   }       @State private var selectedOption = 0   @State private var parkingSpot = ""   @State private var trailerNumber = ""   @State private var tagID = ""   @State private var selectedRadio = 0   @State private var entries: [InventoryEntry] = []   @State private var isMailComposerPresented = false   @State private var isErrorMessageShown = false   @State private var errorMessage = ""   @State private var id = ""       var body: some View {     NavigationView {       Form {         Picker(selection: $selectedOption, label: Text("Yard Selection")) {           Text("1").tag(0)           Text("2").tag(1)           Text("3").tag(2)           Text("4").tag(3)         }                   TextField("Parking Spot Number", text: $parkingSpot)         TextField("Trailer Number", text: $trailerNumber)         TextField("Tag ID", text: $tagID)         Picker(selection: $selectedRadio, label: Text("Spot Type")) {           Text("Regular Spot").tag(0)           Text("Overflow Spot").tag(1)         }.pickerStyle(SegmentedPickerStyle())                   VStack {           Button(action: addEntry) {             Text("Add Entry")           }           Button(action: clearLastEntry) {             Text("Clear Last Entry")           }           Button(action: finalizeInventory) {             Text("Finalize Inventory")           }         }                   List {           ForEach(entries) { entry in             Text("(entry.parkingSpot), (entry.trailerNumber), (entry.tagID), (entry.spotType), (entry.date)")           }         }         Text("Total Trailers Counted: (entries.count)")       }       .navigationBarTitle("YMS Inventory Tool")     }       __    func addEntry() {       let entry = InventoryEntry(parkingSpot: parkingSpot,                     trailerNumber: trailerNumber,                     tagID: tagID,                     spotType: selectedRadio == 0 ? "Regular Spot" : "Overflow Spot",                     date: "(Date())",                     id: id)       entries.append(entry)     }__           func clearLastEntry() {       entries.removeLast()     }           func finalizeInventory() {       let csvString = self.entries.map { entry in         "(entry.parkingSpot),(entry.trailerNumber),(entry.tagID),(entry.spotType),(entry.date),(entry.id)"       }.joined(separator: "\n")               let filename = "InventoryData.csv"       let filePath = URL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent(filename)       let data = csvString.data(using: .utf8)               do {         try csvString.write(to: filePath, atomically: true, encoding: .utf8)         print("Inventory data saved to (filePath)")       } catch {         self.errorMessage = "Error saving inventory data: (error.localizedDescription)"                   // Show the error message         let alert = UIAlertController(title: "Error", message: self.errorMessage, preferredStyle: .alert)                   let dismissAction = UIAlertAction(title: "Dismiss", style: .default, handler: nil)         alert.addAction(dismissAction)                   self.present(alert, animated: true, completion: nil)       }     }   } }

struct Previews_ContentView_Previews: PreviewProvider {   static var previews: some View {     /@START_MENU_TOKEN@/Text("Hello, World!")/@END_MENU_TOKEN@/   } }

SchemeBuildError: Failed to build the scheme "YMS Inventory Tool" closure containing a declaration cannot be used with result builder 'ViewBuilder' Compile ContentView.swift (arm64): /ContentView.swift:67:14: error: closure containing a declaration cannot be used with result builder 'ViewBuilder'     func addEntry() { SwiftUI.ViewBuilder:2:30: note: struct 'ViewBuilder' declared here @resultBuilder public struct ViewBuilder {

please re-post your code inside a code block (use the formatting tools at the bottom of the text entry field). It is too difficult for anyone to read it formatted this way. Also, tag your post with the platform you are developing for (macOS, iOS, watchOS, iPadOS, tvOS)

You're new to the forum, welcome.

There are several errors in your code.

The first is that func must be outside of the body.

Second, you cannot call alert in SwiftUI (need either viewRepresentatble or the way I sketched in code).

Here is the code, formatted:

import SwiftUI
import MessageUI
import Foundation

struct ContentView: View {
    struct InventoryEntry: Identifiable {
        var id = UUID()
        let parkingSpot: String
        let trailerNumber: String
        let tagID: String
        let spotType: String
        let date: String
    }
    
    @State private var selectedOption = 0
    @State private var parkingSpot = ""
    @State private var trailerNumber = ""
    @State private var tagID = ""
    @State private var selectedRadio = 0
    @State private var entries: [InventoryEntry] = []
    @State private var isMailComposerPresented = false
    @State private var isErrorMessageShown = false
    @State private var errorMessage = ""
    @State private var id = ""
    
    func addEntry() {
        // parameters (id) must be in same order as declaration, but no need for id as it is set in declaration
        let entry = InventoryEntry(parkingSpot: parkingSpot,
                                   trailerNumber: trailerNumber,
                                   tagID: tagID,
                                   spotType: selectedRadio == 0 ? "Regular Spot" : "Overflow Spot",
                                   date: "(Date())"
                                   )
        entries.append(entry)
    }
    
    func clearLastEntry() {
        entries.removeLast()
    }
    
    func finalizeInventory() {
        let csvString = self.entries.map { entry in
            "(entry.parkingSpot),(entry.trailerNumber),(entry.tagID),(entry.spotType),(entry.date),(entry.id)"
       }.joined(separator: "\n")
        let filename = "InventoryData.csv"
        let filePath = URL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent(filename)
        let data = csvString.data(using: .utf8)
        do {
            try csvString.write(to: filePath, atomically: true, encoding: .utf8)
            print("Inventory data saved to (filePath)")
        } catch {
            self.errorMessage = "Error saving inventory data: (error.localizedDescription)"
            // Show the error message
            
            // That's not the way to do in SwiftUI.
            // You should declare a State var someError
            // And display a Text when true
//            let alert = UIAlertController(title: "Error", message: self.errorMessage, preferredStyle: .alert)
//            let dismissAction = UIAlertAction(title: "Dismiss", style: .default, handler: nil)
//            alert.addAction(dismissAction)
//            self.present(alert, animated: true, completion: nil)
        }
    }

    var body: some View {
        NavigationView {
            Form {
                Picker(selection: $selectedOption, label: Text("Yard Selection")) {
                    Text("1").tag(0)
                    Text("2").tag(1)
                    Text("3").tag(2)
                    Text("4").tag(3)
                }
                TextField("Parking Spot Number", text: $parkingSpot)
                TextField("Trailer Number", text: $trailerNumber)
                TextField("Tag ID", text: $tagID)
                Picker(selection: $selectedRadio, label: Text("Spot Type")) {
                    Text("Regular Spot").tag(0)
                    Text("Overflow Spot").tag(1)
                }.pickerStyle(SegmentedPickerStyle())
                VStack {
                    Button(action: addEntry) {
                        Text("Add Entry")
                    }
                    Button(action: clearLastEntry) {
                        Text("Clear Last Entry")
                    }
                    Button(action: finalizeInventory) {
                        Text("Finalize Inventory")
                    }
                }
                List {
                    ForEach(entries) { entry in
                        Text("(entry.parkingSpot), (entry.trailerNumber), (entry.tagID), (entry.spotType), (entry.date)")
                    }
                }
                Text("Total Trailers Counted: (entries.count)")
            }       .navigationBarTitle("YMS Inventory Tool")
        }
        
    }
}
closure containing a declaration cannot be used with result builder 'ViewBuilder'
 
 
Q