gpt4 book ai didi

ios - 如何删除根据重量结果生成的文本?

转载 作者:行者123 更新时间:2023-11-28 11:44:56 26 4
gpt4 key购买 nike

我已经设法调用 HealthKit 来获取最新的体重数据。但是,当我运行它时,它会包含如下整个文本负载:

64 kg E457AF14-36D7-4547-AFAA-EF23DDD6642D "Health" (12.0), "iPhone10,4" (12.0)metadata: {
HKWasUserEntered = 1;
} (2018-10-12 13:12:00 +0100 - 2018-10-12 13:12:00 +0100)

这是我的代码:

    func getTodaysWeight(completion: @escaping (HKQuantitySample) -> Void) {

guard let weightSampleType = HKSampleType.quantityType(forIdentifier: .bodyMass) else {
print("Body Mass Sample Type is no longer available in HealthKit")
return
}

//1. Use HKQuery to load the most recent samples.
let mostRecentPredicate = HKQuery.predicateForSamples(withStart: Date.distantPast,
end: Date(),
options: [])
let sortDescriptor = NSSortDescriptor(key: HKSampleSortIdentifierStartDate,
ascending: false)
let limit = 1
let sampleQuery = HKSampleQuery(sampleType: weightSampleType,
predicate: mostRecentPredicate,
limit: limit,
sortDescriptors: [sortDescriptor]) { (query, samples, error) in


//2. Always dispatch to the main thread when complete.
DispatchQueue.main.async {
guard let samples = samples,
let mostRecentSample = samples.first as? HKQuantitySample else {
print("getUserBodyMass sample is missing")
return
}
completion(mostRecentSample)
}
}
HealthStore.execute(sampleQuery)
}



////////////////////////////////////


private func updateWeightCountLabel() {
getTodaysWeight { (result) in
print("\(result)")
DispatchQueue.main.async {
self.totalWeight.text = "\(result)"
self.totalWeight.text = String(format:"%.2f")
print("\(result)")
}
}
}

我试过先截断结尾。很明显,所有这些文本仍然存在,但它很乱而且不能真正接受。

然后,我尝试在私有(private)函数下方添加以下代码,就像我在另一个 ViewController 中使用的那样,将其设置为两位小数。但是,使用下面的代码只会导致 UIlabel 字符串中不显示任何结果。

self.totalWeight.text = String(format:"%.2f")
print("\(result)")

这是我第一次尝试任何类型的编程,所以我个人使用这个第一个应用程序作为一个持续的学习过程,在我进行的过程中慢慢添加、更改、破坏。

最佳答案

您正在打印该完成处理程序的结果,即 HKQuantitySample。该对象包含多个属性,因此打印整个内容将为您提供您当前看到的包含有关该对象的所有信息的输出。尝试为测量打印 result.quantity。看看this Apple 关于该类型的页面。

关于ios - 如何删除根据重量结果生成的文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52779532/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com