gpt4 book ai didi

ios - 我如何获取 healthkit 的最后 10 天信息?

转载 作者:行者123 更新时间:2023-11-29 00:10:21 31 4
gpt4 key购买 nike

我正在从我的 objective c 应用程序中读取我的 iPhone 的健康值。我只需要阅读最近 10 天的内容,但我无法通过查询来执行此操作。

我正在尝试使用以下代码将查询添加到谓词中:

NSPredicate *explicitforDay =
[NSPredicate predicateWithFormat:@"%K >= %@ AND %K <= %@",
HKPredicateKeyPathDateComponents, startDateComponents,
HKPredicateKeyPathDateComponents, endDateComponents];

然后我尝试了这个:

NSPredicate *explicitforDay = [NSPredicate predicateWithFormat:[NSString stringWithFormat:@"%@ < %%@ AND %@ >= %%@", HKPredicateKeyPathStartDate
, HKPredicateKeyPathEndDate], myFirstDate, myEndDate];

但在输出中我可以看到这样的内容:

(startDate < CAST(529279732.871222, "NSDate") AND endDate >= CAST(527983732.871222, "NSDate"))

为什么打印 CAST 和错误的日期值?谢谢!

最佳答案

Swift 4 解决方案:

在我的示例中,我使用了方便的方法 HKQuery.predicateForSamples,这是一个针对开始和结束日期在指定时间间隔内的样本的谓词。

你需要阅读最后 10 个以下是获取从现在起 -10 天后的日期的方法 Date()

前 -10 天

开始日期是:Calendar.current.date(byAdding: .day, value: -10, to: Date())!

注意:您可以使用您想要的下一天和前几天的日期,只需更改 Date()。

endDate 是:Date() -->(当前日期)

所以我的谓词看起来像一个指定的时间间隔。

 let predicate = HKQuery.predicateForSamples(
withStart: startDate.beginningOfDay(),
end: endDate,
options: [.strictStartDate,.strictEndDate])

正如您在我的谓词中看到的那样,我正在添加 beginningOfDay() 方法,这将允许我从 00:00 开始日期

beginningOfDay() 方法说明:

func beginningOfDay() -> Date {
let beginningOfDay = Calendar.current.startOfDay(for: self)
return beginningOfDay
}

您还可以创建谓词格式字符串来创建等效谓词,如 heathkitDoc 中所述.

let explicitTimeInterval = NSPredicate(format: "%K >= %@ AND %K < %@",
HKPredicateKeyPathEndDate, myStartDate,
HKPredicateKeyPathStartDate, myEndDate)

希望能成功。

关于ios - 我如何获取 healthkit 的最后 10 天信息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46655815/

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