I have a Core Data Model with 3 entities: Employee, Athlete, and Therapist. Employee is an abstract entity, and as such I've marked Employee as the other two entity's parent in Xcode. The Codegen for each entity is set to Class Definition
. Right now I'm writing logic that...
I'm stuck on 3. I've read through Apple's documentation and see that Cloudkit adds an attribute to each record called CD_entityName
containing the appropriate String ("Athlete" or "Therapist") for the entity name defined in Core Data..but I'm still confused. How do I get at the record's entity name and use it to inform my View logic?
Since CD_entityName
exists in CloudKit's representation of the data model, do I need to also create an attribute for CD_entityName
in my Core Data model?
Below is my ContentView.swift
file. When I try to build the app I get an error saying Employee has no member 'CD_entityName'.
struct ContentView: View {
@Environment(\.managedObjectContext) private var viewContext
@FetchRequest(sortDescriptors: [NSSortDescriptor(keyPath: \Employee.date_added, ascending: false)], animation: .default)
private var allEmployees: FetchedResults<Employee>
// * Body * //
var body: some View {
List {
ForEach(allEmployees) { employee in
switch employee.CD_entityName {
case "Athlete":
AthleteRowView(athlete: employee as! Athlete)
case "Sound":
TherapistRowView(therapist: employee as! Therapist)
default:
print("Error: Unknown entityName for record")
}
}
}
} // end Body
} // end ContentView
Try the following
switch employee.entity.name {
case "Athlete":
AthleteRowView(athlete: employee as! Athlete)
case "Sound":
TherapistRowView(therapist: employee as! Therapist)
default:
EmptyView()
}