#import "CPPedometerViewController.h"
#import <CoreMotion/CoreMotion.h>
@interface CPPedometerViewController()
@property (weak, nonatomic) IBOutlet UILabel *stepsCountingLabel;
@property (nonatomic, strong) CMStepCounter *cmStepCounter;
@property (nonatomic, strong) NSOperationQueue *operationQueue;
@property (nonatomic, strong) NSMutableArray *stepsArray;
@end
@implementation CPPedometerViewController
- (NSOperationQueue *)operationQueue
{
if (_operationQueue == nil)
{
_operationQueue = [NSOperationQueue new];
}
return _operationQueue;
}
- (void)viewDidLoad
{
[super viewDidLoad];
[self QueryExistingStep];
NSLog(@"steps array = %@", _stepsArray);
}
-(void)QueryExistingStep
{
//get todays date
NSDate *now = [NSDate date];
// get six days ago from today
NSDate *sixDaysAgo = [now dateByAddingTimeInterval:-6*24*60*60];
//array to hold step values
_stepsArray = [[NSMutableArray alloc] initWithCapacity:7];
//check if step counting is avaliable
if ([CMStepCounter isStepCountingAvailable])
{
//init step counter
self.cmStepCounter = [[CMStepCounter alloc] init];
//get seven days before from date & to date.
for (NSDate *toDate = [sixDaysAgo copy]; [toDate compare: now] <= 0;
toDate = [toDate dateByAddingTimeInterval:24 * 60 * 60]) {
//get day before
NSDate *fromDate = [[toDate copy] dateByAddingTimeInterval: -1 * 24 * 60 * 60];
[self.cmStepCounter queryStepCountStartingFrom:fromDate to:toDate toQueue:self.operationQueue withHandler:^(NSInteger numberOfSteps, NSError *error) {
if (!error) {
NSLog(@"queryStepCount returned %ld steps", (long)numberOfSteps);
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
[self updateArrayWithStepCounter:numberOfSteps];
}];
} else {
NSLog(@"Error occured: %@", error.localizedDescription);
}
}];
}
} else {
// stuffhappens
}
}
- (void)updateArrayWithStepCounter:(NSInteger)numberOfSteps {
[_stepsArray addObject:[NSNumber numberWithInteger:numberOfSteps]];
}
@end
我期待有一個數組完整的過去七天的步驟,然後將它們插入到NSinteger爲每一天。例如NSinteger daySeven = 242,NSInteger daySix = 823 ...等等今天。CMStepCounter添加步驟來分離NSIntegers
但是,退出updateArrayWithStepCounter方法後數組似乎已清除。任何關於如何解決這個問題的想法,以便每個步驟都進入單獨的NSIntegers。謝謝,瑞安。
編輯:
這裏是輸出的NSLog:
2014-01-25 22:51:36.314 Project[6633:60b] steps array = (
)
2014-01-25 22:51:36.332 Project[6633:420f] queryStepCount returned 3505 steps
2014-01-25 22:51:36.334 Project[6633:420f] queryStepCount returned 3365 steps
2014-01-25 22:51:36.335 Project[6633:420f] queryStepCount returned 7206 steps
2014-01-25 22:51:36.337 Project[6633:420f] queryStepCount returned 6045 steps
2014-01-25 22:51:36.339 Project[6633:420f] queryStepCount returned 5259 steps
2014-01-25 22:51:36.342 Project[6633:420f] queryStepCount returned 6723 steps
2014-01-25 22:51:36.344 Project[6633:420f] queryStepCount returned 440 steps
這裏是所建議所示的輸出。正如你所看到的,當它在運行該方法後檢查數組時,它肯定會獲取值,但現在它是空的。我可以不正確地將它添加到數組嗎?
我希望這更清楚我很難過。謝謝
通過它回來到viewDidLoad方法時_stepsArray打印出空:( – Ryan
這不的問題提供一個答案,以批判或作者作出澄清,離開低於其信息的評論中 - 你可以隨時評論你自己的帖子,一旦你有足夠的[聲譽](http://stackoverflow.com/help/whats-reputation)你將能夠[評論任何帖子](http://stackoverflow.com/help/privileges/comment) –
我最初發送了一個問題,因爲我沒有足夠的聲望發佈對他的問題的評論,但我只是擴展了我的答案:-) – Daniele