从 HealthKit 拉取时的额外字符
Extra Characters when pulling from HealthKit
当我使用 HKSampleQuery 提取我的 HealthKit 数据时,我创建了一个数组,然后填充了一个表视图。但是,当我这样做时,我的 tableViewCell 在血糖值之后有许多其他字符。这是单元格的屏幕截图:
这是我查询数据的地方。请帮忙!
let endDate = NSDate()
let startDate = NSCalendar.current.date(byAdding: .day, value: number, to: endDate as Date)
let sampleType = HKSampleType.quantityType(forIdentifier: HKQuantityTypeIdentifier.bloodGlucose)
let mostRecentPredicate = HKQuery.predicateForSamples(withStart: startDate, end: endDate as Date, options: [])
let query = HKSampleQuery(sampleType: sampleType!, predicate: mostRecentPredicate, limit: HKObjectQueryNoLimit, sortDescriptors: nil) { (query, results, error) in
if let results = results as? [HKQuantitySample] {
self.bloodGlucose = results
}
DispatchQueue.main.async {
self.tableView.reloadData()
}
}
healthStore.execute(query)
这是我设置表格视图的地方...
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return bloodGlucose.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let currentCell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
let sugar = bloodGlucose[indexPath.row]
currentCell.textLabel?.text = "\(sugar)"
currentCell.detailTextLabel?.text = dateFormatter.string(from: sugar.startDate)
return currentCell
}
您似乎在显示 HKQuantitySample
的整个字符串表示形式。如果只想显示 HKQuantitySample
的数量,为什么不使用它的 quantity
属性?
currentCell.textLabel?.text = "\(sugar.quantity)"
顺便说一下,您最好将 endDate
声明为 Date
(而不是 NSDate
):
let endDate = Date()
let startDate = Calendar.current.date(byAdding: .day, value: number, to: endDate)
let sampleType = HKSampleType.quantityType(forIdentifier: .bloodGlucose)
let mostRecentPredicate = HKQuery.predicateForSamples(withStart: startDate, end: endDate)
通常,非 NS
类型与 Swift 配合使用效果更好。
当我使用 HKSampleQuery 提取我的 HealthKit 数据时,我创建了一个数组,然后填充了一个表视图。但是,当我这样做时,我的 tableViewCell 在血糖值之后有许多其他字符。这是单元格的屏幕截图:
这是我查询数据的地方。请帮忙!
let endDate = NSDate()
let startDate = NSCalendar.current.date(byAdding: .day, value: number, to: endDate as Date)
let sampleType = HKSampleType.quantityType(forIdentifier: HKQuantityTypeIdentifier.bloodGlucose)
let mostRecentPredicate = HKQuery.predicateForSamples(withStart: startDate, end: endDate as Date, options: [])
let query = HKSampleQuery(sampleType: sampleType!, predicate: mostRecentPredicate, limit: HKObjectQueryNoLimit, sortDescriptors: nil) { (query, results, error) in
if let results = results as? [HKQuantitySample] {
self.bloodGlucose = results
}
DispatchQueue.main.async {
self.tableView.reloadData()
}
}
healthStore.execute(query)
这是我设置表格视图的地方...
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return bloodGlucose.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let currentCell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
let sugar = bloodGlucose[indexPath.row]
currentCell.textLabel?.text = "\(sugar)"
currentCell.detailTextLabel?.text = dateFormatter.string(from: sugar.startDate)
return currentCell
}
您似乎在显示 HKQuantitySample
的整个字符串表示形式。如果只想显示 HKQuantitySample
的数量,为什么不使用它的 quantity
属性?
currentCell.textLabel?.text = "\(sugar.quantity)"
顺便说一下,您最好将 endDate
声明为 Date
(而不是 NSDate
):
let endDate = Date()
let startDate = Calendar.current.date(byAdding: .day, value: number, to: endDate)
let sampleType = HKSampleType.quantityType(forIdentifier: .bloodGlucose)
let mostRecentPredicate = HKQuery.predicateForSamples(withStart: startDate, end: endDate)
通常,非 NS
类型与 Swift 配合使用效果更好。