@FetchedResults not giving the correct compiler hint?

We raised a bug on this, but just double checking we are not doing something silly, has anyone seen this?


We set up a FetchRequest like this:


@FetchRequest(entity: Activity.entity(),
              sortDescriptors: [NSSortDescriptor(keyPath: \Activity.name, ascending: true)],
              predicate: NSPredicate(value: true),
              animation: .default) var test : FetchedResults


and then try to use it in a view:

ForEach (test) { activity in
     Text(activity.name)
}


But get the following compile-time bug:


*** Value of type 'ServerObject' has no member 'name'


Now the type of the entity is "Activity" defined as:


@objc(Activity)
public class Activity: ControlledResource {

...

@objc(ControlledResource)
public class ControlledResource: ServerObject {

...

@objc(ServerObject)
publicclass ServerObject: NSManagedObject, Identifiable {


And "Actifvity" definitely has "name", this is an existing app and core data model being migrated to swiftui. "name" works in the original code.


But it seems from the compiler error that the compiler thinks its the superclass type (ServerObject) being returned which does NOT have "name".


Is it possible the compiler is looking for the "Identifiable" conformant super class and not correctly working out the explicit subclass type is actually the object being fetched?

Replies

OK, so I tried moving the Identifiable to the Activity class, no dice


Then I switched to a field off the ServerObject class and it compiles and works fine.


So I think definitely a bug.

Any luck if you change your declaration to be as follows:


@FetchRequest(entity: Activity.entity(),  
              sortDescriptors: [NSSortDescriptor(keyPath: \Activity.name, ascending: true)],  
              predicate: NSPredicate(value: true),  
              animation: .default) var test : FetchedResults<Activity> 


Adding the <Activity> annotation? That's to disambiguate what it's a collection of.

Ah, sorry, I didn't notice the copy-paste had truncated that. It was already there, so that hasn't helped.


This is the definition I am using:


@FetchRequest(entity: Activity.entity(),
               sortDescriptors: [NSSortDescriptor(keyPath: \Activity.name, ascending: true)],
               predicate: NSPredicate(value: true),
               animation: .default) var test : FetchedResults<Activity>


So, no, it doesn't work and reports that error as above.


But thanks for picking that up.

OK, even more weird, it just sort of started working some time after the last beta was released. So I put it down to that...


... Until I set up another one, now THAT new one will not work and reports the same error, but the first example that suddenly started working is still working!!!


I will go over the 2 again with a fine-toothed comb, but I am certain I already triple checked and they are the same.

I had a similar error message which in my case turned out to be because the property I was trying to display in the Text view was an optional String. I got a proper error message by explicitly declaring the type of the closure parameter, i.e.


ForEach(test) { (activity: Activity) in  
     Text(activity.name)  
} 
Post not yet marked as solved Up vote reply of poya Down vote reply of poya