I have a Core Data model with an abstract entity called Shape
. Shape
is a parent entity of Cube
, Pyramid
, Prism
, Cylinder
, and Sphere
. Each of these child entities has a “volume” attribute.
I want to retrieve every child instance of Shape
that has the highest volume. However, I want to avoid writing an enum to loop through each and every subclass, as this approach is not maintainable. If a new shape is added later on and the enum is not updated, some Shape
entities might be missed.
public enum Shape: CaseIterable {
case cube
case pyramid
case sphere
case prism
}
Is there a way to leverage polymorphism to fetch all child entities of “Shape” with the highest volume without explicitly enumerating each subclass? This would ensure that the code remains flexible and adaptable to future additions of new shape entities.
I found a similar question that explains how to retrieve child entities alone, but it was asked before the introduction of Swift so the solution is in Objective-C and it’s not entirely relevant with what I am trying to achieve. I am interested in a modern approach that leverages polymorphism using Swift.