title: SwiftUI: ForEach of FetchedResults gives the error - "Value of type 'NSManagedObject' has no member..."
body: <p>I'm new to SwiftUI and partially to Swift, so hope someone can shed some light on something I am blatantly missing. Excuse the messy code! I haven't had chance yet to refactor.</p>
  
  <p>I'm using a <code>@FetchRequest</code> property wrapper to pull a list of all <code>Memories</code> back from a <code>CoreData</code> model, with a <code>NSFetchRequest&lt;Memory&gt;</code> method defined on it, extending from <code>NSManagedObject</code>.</p>
  
  <p>When I iterate through the results of the <code>FetchRequest&lt;Memory&gt;</code> in SwiftUI via a <code>ForEach</code>, it returns an instance of <code>NSManagedObject</code> into the loop variable and not of <code>Memory</code>, and thus displays the error: <code>Value of type 'NSManagedObject' has no member 'createdAt'</code> for the following SwiftUI class:</p>
  
  <pre class="lang-swift prettyprint-override"><code>import SwiftUI
  
  struct MemoryView: View {
  
      @Environment(
          \.managedObjectContext
      ) var managedObjectContext
  
      @FetchRequest(
          fetchRequest: Memory.allMemories()
      ) var allMemories: FetchedResults&lt;Memory&gt;
  
      @State private var newMemory = ""
  
      var body: some View {
          NavigationView {
              List {
                  Section(header: Text("What's on your mind?")) {
                      HStack {
                          TextField("New memory", text: self.$newMemory)
  
                          Button(action: {
                              let memory = Memory(context: self.managedObjectContext)
  
                              memory.title = self.newMemory
                              memory.createdAt = Date()
  
                              do {
                                  try self.managedObjectContext.save()
                              } catch {
                                  print(error)
                              }
  
                              self.newMemory = ""
                          }) {
                              Image(systemName: "plus.circle.fill")
                                  .foregroundColor(.green)
                                  .imageScale(.large)
                          }
                      }
                  }
                  .font(.headline)
  
                  Section(header: Text("My Memories")) {
                      ForEach(self.allMemories) { memoryItem in
                          MemoryRow(title: memoryItem.title, createdAt: memoryItem.createdAt)
                          //               /\ /\ /\ /\ /\ /\            /\ /\ /\ /\ /\ /\ /\
                          // &gt;&gt;&gt; Error:
                          // &gt;&gt;&gt; Value of type 'NSManagedObject' has no member 'createdAt'
                      }
                  }
              }
              .navigationBarTitle(Text("Memories"))
              .navigationBarItems(trailing: EditButton())
          }
      }
  }
  
  struct MemoryView_Previews: PreviewProvider {
      static var previews: some View {
          MemoryView()
      }
  }
  </code></pre>
  
  <p>And for reference, the <code>Memory</code> model in question is as follows:</p>
  
  <pre class="lang-swift prettyprint-override"><code>import Foundation
  import CoreData
  import CoreLocation
  
  // Define the CoreData Object Model.
  // Ensuring that this class name matches
  // the entity model name.
  public class Memory: NSManagedObject, Identifiable {
      @NSManaged public var title: String?
      @NSManaged public var body: String?
  
      @NSManaged public var latitude: Double
      @NSManaged public var longitude: Double
  
      @NSManaged public var createdAt: Date?
      @NSManaged public var updatedAt: Date?
  
  //    @NSManaged public var type: Type?
  //    @NSManaged public var tags: [Tags]?
  }
  
  // Create an extension in which to place things out of the context
  // of the initial class declaration within.
  extension Memory {
      // Define a helper method for accessing a generated
      // CLLocationCoordinate2D instance from the objects
      // latitude and longitude values.
      var locationCoordinate: CLLocationCoordinate2D {
          CLLocationCoordinate2D(
              latitude: latitude,
              longitude: longitude
          )
      }
  
      // Define a helper method that grabs all instances of a Memory
      // sorted by their created date in ascending order.
      static func allMemories() -&gt; NSFetchRequest&lt;Memory&gt; {
          let request: NSFetchRequest&lt;Memory&gt; = Memory.fetchRequest() as! NSFetchRequest&lt;Memory&gt;
  
          let sortDescriptor = NSSortDescriptor(key: "createdAt", ascending: true)
  
          request.sortDescriptors = [sortDescriptor]
  
          return request
      }
  }
  </code></pre>
  
  <p>Any help on this would be greatly appreciated as I am all out of ideas. A fresh head for tomorrow's problem me thinks!</p>
  
  <p>Edit: Here is the <code>MemoryRow</code> class:</p>
  
  <pre class="lang-swift prettyprint-override"><code>import SwiftUI
  
  struct MemoryRow: View {
      var title: String = ""
      var createdAt: String = ""
  
      var body: some View {
          HStack {
              VStack(alignment: .leading) {
                  Text(title)
                      .font(.headline)
  
                  Text(createdAt)
                      .font(.caption)
              }
          }
      }
  }
  
  struct MemoryRow_Previews: PreviewProvider {
      static var previews: some View {
          MemoryRow(title: "Test Title", createdAt: "Test Date")
      }
  }
  </code></pre>