I'm new in Swift and CoreData and I have a question on how to read data that was stored in CoreData (NsManagedObject) and save it to an data array which can than be used as input for a SwiftUI Line Chart.
My test data model is simple:
@NSManaged public var date: Date?
@NSManaged public var value: Double
I'm able to read the number of values by using this code:
private func fetchItem() {
withAnimation {
let fetchRequest: NSFetchRequest<Rate> = Rate.fetchRequest()
do {
let fetchedResults = try viewContext.fetch(fetchRequest)
print("#Entities: \(fetchedResults.count)")
} catch {
print(error)
}
}
}
Now I need the double values from Core Data stored in an array. For the Line Chart demo (iLineChart) I have the following test data array:
let data = (1...100).map( {_ in Double.random(in: -1...10)} )
Question:
How can I create an array with double values from the NSManagedObject values?
Regards, Ralf