\.self as an id in Swiftui ForEach: refers to parent Core Data class

Hi all,

I hope you can help me on an issue about Core Data Entity inheritance and id parameter in SwiftUI ForEach

My data model has « Person » as the parent class of class « Kid»
Person has id: UUID has an attribute, so Kid inherits this attribute

Kid has an attribute « schoolName ». Person does not have this attribute

Kid has a relationship with Toy (many to many)
In the class Kid, I have a class function :
Code Block
@nonobjc public class func kids(for toy: Toy) -> [Kid]


In a toys dedicated swiftUI view, I want to list the schools of the kids playing with a given toy

So, I have this :
Code Block
ForEach(Kid.kids(for: toy), id: \.self) { kid in
Text(kid.schoolName)
}


The issue is that Xcode associate id: \.self to the class Person and not to Kid
Hence, the line Text(kid.schoolName) gives the compiler error :
Value of type ‘Person’ has no member ‘schoolName

My question is: which value should I give to the id parameter in ForEach so that kid in the closure is actually of Kid Type ? Or what else should I do in order for the ForEach to loop on kids, not on persons ?

I am using Xcode 11.6

Any helps would be much appreciated
Hi All

Well, it seems this issue is a problem only for me.
I am still struggling with the fact of id refereing to the parent Person class instead of the Kid class

I am clueless ... please help :-)

\.self as an id in Swiftui ForEach: refers to parent Core Data class
 
 
Q