title: Getting the total for a number of fields
body: <p>I have an app that records costs for a car.  I can't work out how to create a field that keeps a running total for the ongoing costs.  In the ContentView file I have a struct that defines what an expense is, which includes the 'amount'.</p>
  <p>Any help is appreciated.  Thanks.</p>
  <p>There are 2 files, ContentView, and Addview;</p>
  <pre><code>
  struct ContentView: View {
      
      @StateObject var expenseList = ExpenseList()
      @State private var isShowingAddView = false
      @State private var totalCost = 0.0
          
      var body: some View {
          NavigationView {
              VStack {
                  VStack(alignment: .trailing) {
                      Text(&quot;Total Cost&quot;).font(.headline) //just holding a place for future code
                  }
                  Form {
                      List {
                          ForEach(expenseList.itemList) { trans in
                              HStack{
                                  Text(trans.item)
                                      .font(.headline)
                                  Spacer()
                                  VStack(alignment: .trailing) {
                                      
                                      HStack {
                                          Text(&quot;Amount: &quot;)
                                              .font(.caption).bold()
                                          Text(trans.amount, format: .currency(code: &quot;USD&quot;))
                                              .font(.caption)
                                      }
                                  }
                              }
                          }
                          .onDelete(perform: removeItems)
                      }
                  }
                  .navigationTitle(&quot;Expenditure&quot;)
                  .toolbar {
                      Button {
                          isShowingAddView = true
                      } label: {
                          Image(systemName: &quot;plus&quot;)
                      }
                  }
                  .sheet(isPresented: $isShowingAddView) {
                      AddView(expenseList: expenseList)
                  }
              }
          }
      }
      
      func removeItems(at offsets: IndexSet) {
          expenseList.itemList.remove(atOffsets: offsets)
      }
  }
  
  class ExpenseList: ObservableObject {
      
      @Published var itemList = [ExpenseItem]() {
          didSet {
              if let encoded = try? JSONEncoder().encode(itemList) {
                  UserDefaults.standard.set(encoded, forKey: &quot;Things&quot;)
              }
          }
      }
      init() {
          if let savedItems = UserDefaults.standard.data(forKey: &quot;Things&quot;) {
              if let decodedItems = try? JSONDecoder().decode([ExpenseItem].self, from: savedItems) {
                  itemList = decodedItems
                  return
              }
          }
          itemList = []
      }
  }
  
  struct ExpenseItem: Identifiable, Codable {
      var id = UUID()
      let item: String
      let amount: Double
  }
  
  struct ContentView_Previews: PreviewProvider {
      static var previews: some View {
          ContentView()
      }
  }
  </code></pre>
  <pre><code>
  import SwiftUI
  
  struct AddView: View {
      
      @Environment(\.dismiss) var dismiss
      @ObservedObject var expenseList: ExpenseList
      @State private var item = &quot;Fuel&quot;
      @State private var amount = 0.0
      let itemType = [&quot;Fuel&quot;, &quot;Tyres&quot;]
  
      var body: some View {
          NavigationView {
              Form {
                  Picker(&quot;Type&quot;, selection: $item) {
                      ForEach(itemType, id: \.self) {
                          Text($0)
                      }
                  }
                  TextField(&quot;Enter amount...&quot;, value: $amount, format: .currency(code: &quot;USD&quot;))
              }
              .navigationTitle(&quot;Add an item...&quot;)
              .toolbar {
                  Button(&quot;Save&quot;) {
                      let trans = ExpenseItem(item: item, amount: amount)
                      expenseList.itemList.append(trans)
                      dismiss()
                  }
              }
          }
      }
  }
  
  struct AddView_Previews: PreviewProvider {
      static var previews: some View {
          AddView(expenseList: ExpenseList())
      }
  }
  
  </code></pre>