Skip to content

Instantly share code, notes, and snippets.

@danielctull
Last active August 29, 2015 14:06
Show Gist options
  • Save danielctull/44b5809b4ae7a4826661 to your computer and use it in GitHub Desktop.
Save danielctull/44b5809b4ae7a4826661 to your computer and use it in GitHub Desktop.
HKSampleQuery returns an array of AnyObject as a result. Now, I'm almost guaranteed that it will be an array of HKQuantitySample objects. I would like to end up with a list of NSDate objects for dates I already have Health sample data for that. I figure I can filter the results array to only include HKQuantitySample objects and then map them to …
import HealthKit
let quantityType = HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierBodyMass)
let query = HKSampleQuery(sampleType:quantityType, predicate:nil, limit:0, sortDescriptors:nil) {
query, results, error in
// results is [AnyObject]
let samples = results.filter {
($0 as? HKQuantitySample) != nil
}
// samples is [AnyObject]
// I need the cast here because the filter hasn't changed the type of the array
// Ideally samples would be [HKQuantitySample] going into this function
let dates = samples.map {
($0 as HKQuantitySample).startDate
}
// dates is [NSDate!]
}
// The result is that dates is the array [NSDate!] which then causes issues down the line :-/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment