代码是将活动数据存储到条形图中,但是,最后一行标记了我从未遇到过的错误。
代码:
var days:[String] = []
var stepsTaken:[Int] = []
let activityManager = CMMotionActivityManager()
let pedoMeter = CMPedometer()
let formatter = NSDateFormatter()
formatter.dateFormat = "d MMM"
dispatch_sync(serialQueue, { () -> Void in
let today = NSDate()
for day in 0...6{
let fromDate = NSDate(timeIntervalSinceNow: Double(-7+day) * 86400)
let toDate = NSDate(timeIntervalSinceNow: Double(-7+day+1) * 86400)
let dtStr = formatter.stringFromDate(toDate)
self.pedoMeter.queryPedometerDataFromDate(fromDate, toDate: toDate) { data, error in
if let data = data {
if(error == nil){
print("\(dtStr) : \(data.numberOfSteps)")
self.days.append(dtStr)
self.stepsTaken.append(Int(data.numberOfSteps))
print("Days :\(self.days)")
print("Steps :\(self.stepsTaken)")
if(self.days.count == 7){
dispatch_sync(dispatch_get_main_queue(), { () -> Void in
let xVals = self.days
var yVals: [BarChartDataEntry] = []
for idx in 0...6 {
yVals.append(BarChartDataEntry(value: Float(self.stepsTaken[idx]), xIndex: idx))
}