#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