我有一组来自传感器的事件。该集看起来类似于:
@[{@"type":@1,@"date":"2014-01-01 17:00"},
{@"type":@3,@"date":"2014-01-01 17:01" },
{@"type":@1,@"date":"2014-01-01 17:02" },
{@"type":@1,@"date":"2014-01-01 17:02" },
{@"type":@1, @"date":"2014-01-01 17:03" },
{@"type":@2,@"date":"2014-01-02 17:04" },
{@"type":@2,@"date":"2014-01-02 17:04" },
{@"type":@2,@"date":"2014-01-02 17:04" }
];
因此,有一种类型的事件与其他类型的单一事件混合在一起。我想检测事件的顺序。如果我只查看事件的类型我有一个数组@ [1,3,1,1,2,2,1,2] 我可以清楚地看到3和1不属于那里,事件的顺序应该是: @ [1,2]看起来最简单的方法就是删除那些不匹配的事件。
-(NSInteger)locationCheckForProximityEvents:(NSArray *)proximityEvents andDate:(NSDate *)date{
NSInteger accuracy = 7; // time in seconds to be evaluated
NSArray * lastEvents = [proximityEvents filteredArrayUsingPredicate:[NSPredicate predicateWithBlock:^BOOL(NSDictionary * evaluatedObject, NSDictionary *bindings) {
NSDate * edate = [evaluatedObject objectForKey:@"date"];
NSInteger proximity =[[evaluatedObject objectForKey:@"proximity"] integerValue];
NSInteger diff = [date timeIntervalSinceDate:edate];
if(diff<accuracy&&(proximity!=CLProximityUnknown)){
return YES;
}
return NO;
}]];
//we are returning -1
if(lastEvents.count == 0) return -1;
NSInteger first= lastEvents.count <accuracy ? 0:lastEvents.count-accuracy;
NSInteger count =lastEvents.count > accuracy? accuracy:lastEvents.count;
NSArray * lastTen = [lastEvents subarrayWithRange: NSMakeRange(first
, count) ];
NSArray * beds = [lastTen filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"(type==%@)",@(kbed)]];
NSArray * sinks = [lastTen filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"(type==%@)",@(ksink)]];
NSArray * briefings = [lastTen filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"(type==%@)",@(kbriefing)]];
NSArray * simlabs = [lastTen filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"(type==%@)",@(kroom)]];
if(beds.count >0 && beds.count>= (count -1)/2){
return kbed;
}
if(briefings.count > 0 && briefings.count > count/2){
return kbriefing;
}
if(simlabs.count>0 && simlabs.count > count/2){
return kroom;
}
return -1;
}