Problem one is to match NSNumber doubles within some tolerance. That would look something like this:
- (NSComparisonResult)number:(NSNumber *)nA nearlyEquals:(NSNumber *)nB within:(double)epsilon {
double diff = [nA doubleValue] - [nB doubleValue];
if (fabs(diff) < epsilon) return NSOrderedSame;
else return (diff < 0.0)? NSOrderedAscending : NSOrderedDescending;
}
Problem two is a more algorithmic. Finding the cue in the array of numbers given an arbitrary time from the player is definitely a search, but it's a search with some very big hints: a) the cues are probably sorted, b) the playback times are appearing sequentially, increasing monotonically.
Keeping just a little state, like the index of the last place where you checked, you should need to do very little or no iterating through your array.
@property (assign, nonatomic) NSInteger lastCheckedIndex;
Somewhere, before you begin, assign self.lastCheckedIndex = -1
. Then, each time your timer fires, get the current playback time, wrap it as [NSNumber numberWithDouble:
, and call something like this:
- (NSInteger)indexOfCueMatching:(NSNumber *)playbackTime in:(NSArray *)cues {
NSInteger index = self.lastCheckedIndex+1;
if (index >= cues.count) return NSNotFound;
NSComparisonResult compResult = [self number:cues[index] nearlyEquals:playbackTime within:0.1];
while (index<cues.count && compResult == NSOrderedAscending) {
compResult = [self number:cues[++index] nearlyEquals:playbackTime within:0.1];
}
self.lastCheckedIndex = index;
return (compResult == NSOrderedSame)? index : NSNotFound;
}
This should answer either the index in your cue array that matches, or NSNotFound if no matching cue is found. Moreover, this should update the lastCheckedIndex knowing that the next time coming from the player will be larger than the last, and the next time in your cue array will be larger as well.
EDIT - specifically, use it like this...
NSTimeInterval playbackTime = self.movieController.currentPlaybackTime;
self.lastRecordedPlaybackTime = playbackTime;
NSNumber *playbackTimeNumber = [NSNumber numberWithDouble:playbackTime];
NSInteger index = [self indexOfCueMatching:playbackTimeNumber in:self.video.cues];
if (index != NSNotFound) {
// whatever we do when we find a matching cue
}
Hope that's helpful.