.propertiestofetch to grab an array of properties in the CoreData entity in Swift

563 Views Asked by At

I am trying to fetch a property in an entity. the property is Double_t type. My goal is to generate an Array of [Double_t] with all members of field "readingNumber", and pass it to prepareForSegue to make a graph in a different view. After run the following code, I was able to print the var DistinctResults, for example, as: ({readingNumber = 222;},{readingNumber = 114;},{readingNumber = 588;}) etc. Now I am facing the difficulty of generating the Array of [222, 114, 588]. Could someone give me a hint? Very much appreciate your help!! Best Regards.

let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
        let managedContext = appDelegate.managedObjectContext!

        let FetchRequest = NSFetchRequest(entityName: "UserReading")
        FetchRequest.propertiesToFetch = ["readingNumber"]
        FetchRequest.resultType = NSFetchRequestResultType.DictionaryResultType
        FetchRequest.returnsDistinctResults = true

            var DistinctResults: NSArray = managedContext.executeFetchRequest(FetchRequest, error: nil)!

            println(DistinctResults)
1

There are 1 best solutions below

1
On

Try this:

var clean: [String] = [""]

clean = [DistinctResults!.valueForKey("readingNumber")][0] as! [String]

...

return clean