I am reading data with following code from CoreData but instead of that can we read first attribute names "firstName", "lastName", "age" from CoreData into an array and read their values instead of writing all the names in code.
It is repeated work because they are written in DataModel as well.
loadData() {
let fetchRequest = NSFetchRequest<NSFetchRequestResult>(entityName: "Entity")
do {
let result = try context.fetch(fetchRequest)
dump(result)
for data in result as! [NSManagedObject] {
fNames = data.value(forKey: "firstName") as! String
lNames = data.value(forKey: "lastName") as! String
age = data.value(forKey: "age") as! Int
print("first (fNames), last : (lNames), last : (age)")
}
} catch {
print("Could not load data: (error.localizedDescription)")
}
}
2
Answers
Try this, access your entity name from
NSManagedObject
e.g.
Use the class that Xcode has generated for you that has the same name as the entity name