我添加这样的示例:
var store:HKHealthStore?
date = NSDate()
let type = HKQuantityType.quantityTypeForIdentifier(HKQuantityTypeIdentifierDietaryVitaminA)
quantity = HKQuantity(unit: HKUnit.gramUnitWithMetricPrefix(.Micro), doubleValue: 100)
let sample = HKQuantitySample(type: type, quantity: quantity, startDate: date, endDate: date)
store.saveObject(sample, withCompletion: { (success, error) -> Void in
if(error != nil) {
println("Error saving sample: \(error.localizedDescription)")
}else{
println("Sample saved successfully!")
}
})
当我想删除这个示例时,我执行:
store.deleteObject(sample, withCompletion: {(success, error) -> Void in
if(error != nil) {
println("Error deleting sample: \(error.localizedDescription)")
}else{
println("Sample deleted successfully!")
}
})
它返回给我:删除样本时出错:找不到对象每次我对 startDate 和 endDate 使用相同的“日期”。我已经尝试过let sample = HKQuantitySample(type: type, quantity: quantity, startDate: date, endDate: date, metadata: metadata)
元数据在哪里let metadata = [HKMetadataKeyExternalUUID:"\(Int64(date.timeIntervalSince1970))"]
,但也没有成功......