Post

Replies

Boosts

Views

Activity

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@/   } }
3
0
1.1k
Feb ’23