1)配列をソートします(オプションですが、必要だと言いました)
2) バイナリ検索の代わりに、NSPredicate を使用して、関心のあるエントリを見つけます。
これは、タイムスタンプを持つ独自のクラスに適応する必要がある私のプロジェクトの 1 つからのサンプル コードです。
// this is the property wher you store the data
@property NSArray *data;
// this is a custom struct to hold to timestamp values, the min and max
typedef struct CMTTimeStampRange {
UInt64 min, max;
} CMTTimeStampRange;
// return a sub array with only the objects between two time stamps
- (NSArray *)samplesInTimeStampRange:(CMTTimeStampRange)timeStampRange
{
NSArray *tsRange = @[@(timeStampRange.min), @(timeStampRange.max)];
NSPredicate *filter = [NSPredicate predicateWithFormat:@"timeStamp BETWEEN %@",tsRange];
NSArray *samples = [self.data filteredArrayUsingPredicate:filter];
return samples;
}
アップデート
上記のこのスニペットは、投稿された質問に対する簡単な解決策を提供することを目的としており、高性能コードを意図したものではありません。高いパフォーマンスを得るには、Core Foundation (CFArray) と C 関数を使用することをお勧めします。CFArray には、並べ替えられた CFArray のバイナリ検索である関数 CFArrayBSearchValues があるため、独自の関数を実行する必要はありません。