title: How to read out values of Indexset elements in SwiftUI?
body: <p>I've got a pretty straightforward ForEach List and deleting an entry with onDelete() works without issues. However since one of the variables in the element stores a file's url, that I don't want to leave behind, I'm looking to implement a clean up through FileManager. But for the life of me I can't seem to get access to the actual url to plug into the FileManager action.</p>
  <p>Here's what I've got, with associated errors:</p>
  <pre><code>struct ListItem: Identifiable, Codable {
      let id = UUID()
      let name: String
      let fileurl: URL
  }
  
  class ItemList: ObservableObject {
      @Published var items: [ListItem] {
          didSet {
              let encoder = JSONEncoder()
              if let encoded = try? encoder.encode(items) {
                  UserDefaults.standard.set(encoded, forKey: &quot;Items&quot;)
              }
          }
      }
      
      init() {
          if let items = UserDefaults.standard.data(forKey: &quot;Items&quot;) {
              let decoder = JSONDecoder()
              if let decoded = try? decoder.decode([ListItem].self, from: items) {
                  self.items = decoded
                  return
              }
          }
          
          self.items = []
      }
  }
  
  struct ItemListView: View {
      func getDocumentsDirectory() -&gt; URL {
          return FileManager.default.urls(for: .applicationSupportDirectory, in: .userDomainMask).first!
      }
      @ObservedObject var itemlist = ItemList()
      var supportFolder: URL {
          return FileManager.default.urls(for: .applicationSupportDirectory, in: .userDomainMask).first!
      }
  @EnvironmentObject var thingy: Thingy
      var body: some View {
          VStack {
              Spacer()
              Spacer()
              Spacer()
              
              List {
                  ForEach(itemlist.items) { item in
                      HStack {
                          VStack(alignment: .leading) {
                              Text(item.name)
                                  .font(.headline)
                              Text(item.fileurl.lastPathComponent)
                          }
                          .onTapGesture {
  self.thingy.load(self.supportFolder.appendingPathComponent(&quot;/listfiles/\(item.fileurl.lastPathComponent)&quot;)) //No issues with item.fileurl here
                              print(&quot;Selected: \(item.fileurl)&quot;)
                          }
                      }
                  }
                  .onDelete(perform: removeItems)
                  
              }.disabled(keyMissing)
                  .navigationBarTitle(&quot;List files&quot;, displayMode: .large).accentColor(Color.blue)
                  .navigationBarItems(trailing:
                      Button(action: {
                          …
                      }
                      ) {NavigationLink(…) {
                          Image(systemName: &quot;plus&quot;)
                          }
  
  
                          
                      }
                                          
              )
              Button(…){…}  
          }
              
          .onAppear(perform: {…})
              .onDisappear(…)
      }
      
      
      //    MARK: below is the issue
      func removeItems(at offsets: IndexSet) {
          
     do {
              try FileManager.default.removeItem(atPath: (itemlist.items.fileurl).path)
              print(&quot;File is deleted&quot;)
          }
          catch { print(&quot;Couldn't delete file&quot;)} //This would be ideal, but doesn't work stating &quot;Value of type '[ListItem]' has no member 'fileurl'&quot;
  
  
  
      itemlist.items.remove(atOffsets: offsets) //This works fine
          
         
      }
  }
  
  </code></pre>
  <p>Ideally I would be able to simply plugin item <code>list.items.fileurl</code> but it acts like that's unknown.</p>
  <p>I've also tried to do a huge workaround with <code>list.items.description</code> (and similar) but there's no consistent way to consistently extract the right part of the outpuT String. And to be honest feels rather odd I can't access the value of fileurl, I feel I'm staring the solution in the face but can't see it.</p>
  <p>I've also tried to simply apply the logic at <code>onDelete()</code></p>
  <p>Like so:</p>
  <pre><code>.onDelete{
                      do {
                          try FileManager.default.removeItem(atPath: (binlist.items.fileloc).path)
                          print(&quot;File is deleted&quot;)
                      }
                      catch { print(&quot;Couldn't delete file&quot;)}
                  }
  
  </code></pre>
  <p>But that throws me a <code>Cannot convert value of type '() -&gt; ()' to expected argument type 'Optional&lt;(IndexSet) -&gt; Void&gt;'</code></p>
  <p>I feel a bit out of my element here and can't find good documentation on this.</p>
  <p>Edit: @Asperi's solution worked for me, can't believe I was staring the solution right in the face prior to posting this.</p>