Ios 如何删除权重结果生成的文本?
我设法打电话给HealthKit获取最新的体重数据。但是,当我运行它时,它包含一整堆文本,如下所示:Ios 如何删除权重结果生成的文本?,ios,swift,healthkit,Ios,Swift,Healthkit,我设法打电话给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 getTod
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 func下面添加以下代码,就像我在另一个ViewController中使用的设置为两位小数的代码一样。但是,使用下面的代码只会导致UIlabel字符串中没有显示结果
self.totalWeight.text = String(format:"%.2f")
print("\(result)")
这是我第一次尝试任何类型的编程,所以我个人使用这第一个应用程序作为一个持续的学习过程,慢慢地添加、更改、中断。这只是一个简单的建议 您可以使用字符串
offsetBy
let weight_1 = weight.substring(to:weight.index(weight.startIndex, offsetBy: 6)) // weight is the complex output you got
print(weight_1)
这张照片是:
64 kg
即使这个人体重120公斤,它也会起作用。这只是一个简单的建议 您可以使用字符串
offsetBy
let weight_1 = weight.substring(to:weight.index(weight.startIndex, offsetBy: 6)) // weight is the complex output you got
print(weight_1)
这张照片是:
64 kg
即使此人体重为120公斤,它也应该有效。您正在打印完成处理程序的结果,即
HKQuantitySample
。此对象包含多个属性,因此打印整个对象将为您提供当前看到的输出以及有关该对象的所有信息。试着打印结果。数量
仅用于测量。请看一看Apple关于该类型的页面。您正在打印完成处理程序的结果,即HKQuantitySample
。此对象包含多个属性,因此打印整个对象将为您提供当前看到的输出以及有关该对象的所有信息。试着打印结果。数量
仅用于测量。查看Apple关于该类型的页面。结果的类型是一个HKQuantitySample
,这不仅仅是一个数字,不要解析字符串,而是查找对象的属性,并使用适当的getter/accessor。谢谢。您的解释为我提供了一些有用的术语,可以查看result
的类型是HKQuantitySample
,这不仅仅是一个数字,请不要解析字符串,而是查找该对象具有哪些属性,并使用适当的getter/accessor。谢谢。你的解释给了我一些有用的术语来解释这对我来说非常有效。我将.quantity
添加到self.totalWeight.text=“\(结果)”
。我现在将看一看另一个建议,以便理解它们——我希望这种方法是一种很好的快速修复方法,而另一个建议将修复放在代码的其他地方(我还没有弄清楚),但是一旦我理解了它们,我就可以将学到的东西应用到其他地方。非常感谢。这对我来说非常有效。我将.quantity
添加到self.totalWeight.text=“\(结果)”
。我现在将看一看另一个建议,以便理解它们——我希望这种方法是一种很好的快速修复方法,而另一个建议将修复放在代码的其他地方(我还没有弄清楚),但是一旦我理解了它们,我就可以将学到的东西应用到其他地方。非常感谢。我有一个最初的剧本,但它不断回来,因为“HKQuantitySample”没有成员“substring”,如果我在private func updateWeightCountLabel()
中使用它来修改结果
,我已经阅读了一篇文章,子字符串作为一个概念似乎很简单,所以我还不明白我做错了什么。我应该在其他地方调用它吗?我已经有了一个初始剧本,但它不断回来,因为如果我在private func updateWeightCountLabel()中使用它,“HKQuantitySample”没有成员“substring”
要修改结果
我已经阅读了一篇文章,子字符串作为一个概念似乎很简单,所以我还不明白我做错了什么。我应该在别的地方打电话吗?