Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective-C在NSArray中查找最近NSDate的最快方法_Objective C_Nsarray_Nsdate_Nstimeinterval - Fatal编程技术网

Objective-C在NSArray中查找最近NSDate的最快方法

Objective-C在NSArray中查找最近NSDate的最快方法,objective-c,nsarray,nsdate,nstimeinterval,Objective C,Nsarray,Nsdate,Nstimeinterval,我将NSDate传递给以下函数,并希望在数组中找到时间上最接近传入值的NSDate 请注意,我不想知道数组是否包含如下帖子所示的确切日期: 我想知道数组中哪个日期在时间上最接近我的参考日期 我必须经常这样做。以下方法虽然有效,但速度很慢-如何加快速度 // Get the XYZ data nearest to the passed time - (eciXYZ)eciDataForTime:(NSDate*)time { // Iterate our list and find t

我将NSDate传递给以下函数,并希望在数组中找到时间上最接近传入值的NSDate

请注意,我不想知道数组是否包含如下帖子所示的确切日期:

我想知道数组中哪个日期在时间上最接近我的参考日期

我必须经常这样做。以下方法虽然有效,但速度很慢-如何加快速度

// Get the XYZ data nearest to the passed time
- (eciXYZ)eciDataForTime:(NSDate*)time {

    // Iterate our list and find the nearest time
    float nearestTime = 86400; // Maximum size of dataset ( 2 days )
    int index = 0; // Track the index
    int smallestDifferenceIndex = index; // Track the index with the smallest index
    NSDate *lastListDate; // Track the closest list date
    for ( index = 0 ; index < [self.time count]-1 ; index++ ) {
        NSDate *listDate = [self.time objectAtIndex:index]; // Get the date - Time is an NSMutableArray of NSDates
        // NSTimeInterval is specified in seconds; it yields sub-millisecond precision over a range of 10,000 years.
        NSTimeInterval timeDifferenceBetweenDates = [listDate timeIntervalSinceDate:time];
        if ( timeDifferenceBetweenDates < nearestTime && timeDifferenceBetweenDates > 0 ) {
            nearestTime = timeDifferenceBetweenDates; // Update the tracker
            smallestDifferenceIndex = index; // Update the smallest difference tracker
            lastListDate = listDate; // Capture the closest date match
            //NSLog(@"Time: %f %@",timeDifferenceBetweenDates,listDate);
        }
    }
//获取最接近所传递时间的XYZ数据
-(eciXYZ)eciDataForTime:(NSDate*)时间{
//迭代我们的列表并找到最近的时间
float nearestTime=86400;//数据集的最大大小(2天)
int index=0;//跟踪索引
int smallestDifferenceIndex=index;//使用最小索引跟踪索引
NSDate*lastListDate;//跟踪最近的列表日期
对于(索引=0;索引<[self.time count]-1;索引++){
NSDate*listDate=[self.time objectAtIndex:index];//获取日期-时间是NSDates的NSMutableArray
//NSTimeInterval以秒为单位指定;它在10000年的范围内产生亚毫秒的精度。
NSTimeInterval timeDifferenceBetweenDates=[listDate TimeIntervalsIncestDate:time];
if(数据间的时差<最接近的时间(&T)数据间的时差>0){
nearestTime=数据之间的时间差;//更新跟踪器
smallestDifferenceIndex=index;//更新最小差异跟踪器
lastListDate=listDate;//捕获最接近的日期匹配
//NSLog(@“时间:%f%@”,数据之间的时差,列表日期);
}
}

编辑:我错误地认为
NSMutableOrderedSet
会自动维护顺序。过去我可能使用过一个子类来实现此效果。在NSArray上使用它没有任何好处,除非您需要设置语义

保持集合排序是保持搜索速度的一个好方法。如果可以,请使用
NSOrderedSet
NSMutableOrderedSet
而不是数组对象,否则在添加数组时必须保持数组排序,或者如果只创建了一次,则只需对其排序即可

您还可以使用
nsfastnumeration
更快地枚举任何集合(符合协议)

例如:

// for an NSOrdered set or NSArray of NSDates
for (NSDate* date in self.times) {
    // do something with date
    // cleaner, shorter code too
}
由于您的集合已排序,您现在可以知道最近的日期,而无需迭代整个集合(大多数情况下)

//搜索最接近星期二的日期

[星期日]如果没有重复的日期,则使用集合有效。否则,使用数组有效-只需在添加值时对数组进行排序即可。@rmaddy这是真的,但我认为这不适用于这种情况。没有足够的信息知道它是否适用。更新的答案反映了这一点,谢谢。更快的方法是通过排序后的数组进行二进制搜索而不是上一个示例中的顺序。您必须保持可变有序集的排序方式与可变数组的排序方式相同,因此我不确定您为什么推荐前者而不是后者。
OrderedSet
只是意味着它可以有顺序,但不会自动为您排序元素
// searching for date closest to Tuesday
[Sunday] <- start search here
[Monday] <- one day before
[Thursday] <- two days after, we now know that Monday is closest
[Friday] <- never have to visit
[Saturday] <- never have to visit