我从健康工具包中提取步骤数据,以便将其上传到网站。该网站采用365整数,然后向网站分配倒退日期。所以第一个整数是今天,第二个整数是明天e.t.c.
我正在尝试创建一个存储365个整数的数组,它必须完全填充。我使用Healthkit来确定一年后用户的步数,因此数组大小。每个整数代表1天。
使用健康工具包我从没有任何内容的日子获得日期和步骤以及null / nil。所以我正在考虑制作某种if条件,如果date = date =当前日期然后做一个日期+ 1天循环,但我不知道如何做到这一点。
这是我想存储值的地方,所以我可以稍后用另一个类中的JSON发送它们。
// So this is the first array I am using in order to temporarily hold the arrays, not sure if this is the proper way.
struct stepy {
static var step: [Int] = []
}
// Second and final array that I will use to send the steps in JSON later on
struct stepsofLastyears{
var stepsOfLastYear:[Int] = []
}
// Extracting the step data, irrelevant for my question, but added to add context
func todayTotalSteps () -> Void{
let healthStore: HKHealthStore? = {
if HKHealthStore.isHealthDataAvailable() {
return HKHealthStore()
} else {
return nil
}
}()
let calendar = NSCalendar.current
let interval = NSDateComponents()
interval.day = 1
var anchorComponents = calendar.dateComponents([.day, .month, .year], from: NSDate() as Date)
anchorComponents.hour = 0
let anchorDate = calendar.date(from: anchorComponents)
guard let stepsCount = HKObjectType.quantityType(forIdentifier: HKQuantityTypeIdentifier.stepCount) else {
fatalError("*** Unable to create a step count type ***")
}
// Define 1-day intervals starting from 0:00
let stepsQuery = HKStatisticsCollectionQuery(quantityType: stepsCount, quantitySamplePredicate: nil, options: .cumulativeSum, anchorDate: anchorDate!, intervalComponents: interval as DateComponents)
stepsQuery.initialResultsHandler = {query, results, error in
let endDate = NSDate()
let startDate = calendar.date(byAdding: .day, value: -365, to: endDate as Date, wrappingComponents: false)
if let myResults = results{
myResults.enumerateStatistics(from: startDate!, to: endDate as Date) { statistics, stop in
if let quantity = statistics.sumQuantity(){
let date = statistics.startDate
let steps = quantity.doubleValue(for: HKUnit.count())
// Here is where I attempt to enter the steps into the array
stepy.step.append(Int(steps))
// Here I attempt to fill the second array with 0 for the dates that are null, I was thinking to add the date if check here to place the step value on the correct corresponding value in the array.
stepsofLastyears.stepsOfLastYear = Array(repeating: 0, count: 365)
.enumerated()
.map{ index, value in
guard stepy.step.indices.contains(index)else {return value}
return stepy.step[index]}
print("\(date): steps = \(steps)")
//NOTE: If you are going to update the UI do it in the main thread
DispatchQueue.main.async {
//update UI components
}
}
} //end block
} //end if leta
}
healthStore?.execute(stepsQuery)
我得到的数据是这样的
2017-05-02 22:00:00 +0000: steps = 5544.0
2017-06-02 22:00:00 +0000: steps = 844.0
2017-09-02 22:00:00 +0000: steps = 9999.0
2017-10-02 22:00:00 +0000: steps = 5640.0
2017-10-11 22:00:00 +0000: steps = 6210.0
2017-10-12 22:00:00 +0000: steps = 423.0
2017-10-13 22:00:00 +0000: steps = 555.0
2017-10-14 22:00:00 +0000: steps = 42847.0
2017-10-15 22:00:00 +0000: steps = 555.0
我想获取步骤数据并将其放入数组中,并使数组的第一个值代表今天,即明天的第二个值。 E.t.c