SwiftUI: ForEach of FetchedResults gives the error - "Value of type 'NSManagedObject' has no member..."

In my case, it was because of the optional data type which is by default selected when we add an attribute in data model and therefore in NSManagedObjectClass it looks like this

@NSManaged public var name: String?

and of course, to resolve this one can either remove ? from the above line or use ?? nil coalescing operator when using that attribute like ClassName.name ?? "Unknown"

From the given answers and comments I went to check my attribute name in class for any typo and then I realised the issue. For now have to deal with this behaviour of SwiftUI that it won't show the actual error or will show on any other line.

Hope this can help someone like me.


I'm actually a little confused as to what solved this issue for me. Here are a few things I did before it 'just worked'.

I don't know which one caused it, I just re-opened Xcode and the previously failing build was now building again unexpectedly.

So, in a haste to think what I did to fix it and using the Git diffs as some form of reference, this is me re-tracing my steps, in this order:

  1. Checked the View attachement in the SceneDelegate for the contentView, for which View the NSManagedObjectContext context is exposed onto. This was against the wrong View for me, it was attached to MemoryDetail() not MemoryList().

  2. Deleted the offending line:

MemoryRow(title: memoryItem.title!, createdAt: memoryItem.description)

and replacing it with just a filler Text("test"), saving and building the project successfully.

  1. Changing the sortDescriptor declaration from:
let sortDescriptor = NSSortDescriptor(key: "createdAt", ascending: true)

request.sortDescriptors = [sortDescriptor]

to

request.sortDescriptors = [
    NSSortDescriptor(key: "createdAt", ascending: true)
]
  1. Restarting Xcode (although did this multiple times to no avail during anyway).

  2. Staging, not committing, my tracked code changes.

  3. Deleting the filler Text("test") and manually typing back out the line:

MemoryRow(title: memoryItem.title!, createdAt: memoryItem.description)

In which code hint still does not work, but the line now builds fine, so you have to manually do it. And no, the unwrap force ! was not the cause of the error.


EDIT:

After using Git diffs and pasting in my original code, I think I know what the error is:

MemoryRow(title: memoryItem.title!, createdAt: memoryItem.createdAt)
//                                                        ^^^^^^^^^
// This is wrong ----------------------------------------//////////

It doesn't help as the error the compiler gives isn't directly relational to the actual problem. The error should be that memoryItem.createdAt isn't returning the correct type expected by the MemoryRow.

MemoryRow(title: memoryItem.title!, createdAt: "")
// Compiles fine

// And now the corrected version:
MemoryRow(title: memoryItem.title!, createdAt: "\(memoryItem.createdAt!)")

I think the problem is Xcode's somewhat hap-hazard errors sometimes.

I've since amended the MemoryRow to now accept a Date? type instead of String? to actually better describe what the object is accepting. Along with unwrapping, formatting and presenting the variables within that class.


set NSManagedObject type in closure argument.

ForEach(books, id: \.self) { book in 
     //
}

to

ForEach(books, id: \.self) { (book: Book) in
     //
}

Tags:

Swift

Swiftui