I am trying to fetch data from CoreData and return it as an array for charting in a popular charting library. The code below runs fine and I can print the data to the console, however I want to fill self.hkdataBase with the entries within CoreData and Return it. Any idea what I am doing wrong ?
class ViewController: UIViewController {
var hkdataBase = [HKdataBase]()
....
func fetchCoreData () -> NSArray {
//Setting up Core Data
var context = (UIApplication.sharedApplication().delegate as AppDelegate).managedObjectContext!
var request = NSFetchRequest(entityName: "HKdataBase")
request.returnsObjectsAsFaults = false
//Fetching Data
self.hkdataBase = context.executeFetchRequest(request, error: nil)! as [HKdataBase]
if hkdataBase.count > 0 {
for i in hkdataBase {
**I WANT TO FILL self.hkdataBase WITH i ELEMENTS HERE ** -> println(i.hr_data) WORKS FINE
}
} else {
println("No results")
}
}
**return self.hkdataBase with elements**
}
UPDATE 1: I want to Return an Array from the fetched data in CoreData. The hr_data are Heart Rate measures and hr_date are the associated NSDate. The HKdataBase looks like this:
import Foundation import CoreData
class HKdataBase: NSManagedObject {
@NSManaged var hr_date: NSDate
@NSManaged var hr_data: NSNumber
}
context.executeFetchRequest(...)! as [HKdataBase]is already an array. What else do you need?