2014-01-23 45 views
1

目前正致力于寻找最佳解决方案,以找到当前时间和我的数组内部的日期之间的最小时间间隔。寻找最小的时间间隔(现在和阵列之间的日期)

我有一个方法,需要一个NSArray并返回一个NSArray。该方法执行以下操作:

  1. 循环通过阵列按时间排序(0指数最接近当前时间)
  2. 通过找到三角洲和比较
  3. 抓斗该索引查找最小时间间隔,取它,返回一本字典

我开始看着使用timeIntervalSinceDate等,而不是我下面使用的手动工作。

的阵列看起来是这样的:

tideSummary: [{ 

'type' : 'High Tide', 
'pretty' : 'January 16 at 5:13PM EST', 
'epoch' : '325267782', 

... 

}] 

这段代码似乎是臃肿什么的尝试吗?我觉得根据索引和关键值提取某些数据有很大的重复性?

我想返回阵列中最近的时间,所以我想用timeIntervalSince1970并做一些简单的数学运算来找到最小的增量。我的数组包含返回时间以毫秒计算的一个关键


我如何能清洁任何建议,我的条件,所以我仍然可以提取:lowTideTime - highTideTimetideType

下面是我的方法,我用提取这样的信息:

- (NSArray *)findUpcomingTides: (NSArray *)arrayOfTideCycles { 
    NSTimeInterval currentDateInterval; 
    currentDateInterval = [[NSDate date]timeIntervalSince1970]; 
    NSInteger smallestDelta = currentDateInterval; 
    NSArray *upcomingTideData = [[NSArray alloc] init]; 

    for (NSInteger i = 0; i < arrayOfTideCycles.count; i++) { 

     NSDictionary *eachTideSummary = [arrayOfTideCycles objectAtIndex:i]; 
     NSInteger tideDateAsEPOCH = [[eachTideSummary valueForKeyPath:@"epoch"] intValue]; 
     NSInteger dateDelta = tideDateAsEPOCH - smallestDelta; 
     if (dateDelta < smallestDelta) { 
      smallestDelta = dateDelta; 

      int iPlusOne = i+1; 

      upcomingTide = [arrayOfTideCycles objectAtIndex:i]; 
      NSDictionary *tideTypeDictionary = [arrayOfTideCycles objectAtIndex:i]; 
      tideType = [tideTypeDictionary objectForKey:@"type"]; 

      if([[upcomingTide valueForKeyPath:@"type"] isEqualToString:@"Low Tide"] || [[upcomingTide valueForKeyPath:@"type"] isEqualToString:@"Max Ebb"]){ 

       NSString *lowTidePrettyDateFormat = [upcomingTide valueForKeyPath:@"pretty"]; 
       lowTideTime = [self convertAndFormatDateToTimeWithString:lowTidePrettyDateFormat]; 


       NSDictionary *upcomingHighTide = [arrayOfTideCycles objectAtIndex:iPlusOne]; 
       NSString *highTidePrettyDateFormat = [upcomingHighTide valueForKeyPath:@"pretty"]; 
       highTideTime = [self convertAndFormatDateToTimeWithString:highTidePrettyDateFormat]; 

      } else { 

       NSString *highTidePrettyDateFormat = [upcomingTide valueForKeyPath:@"pretty"]; 
       highTideTime = [self convertAndFormatDateToTimeWithString:highTidePrettyDateFormat]; 

       NSDictionary *upcomingLowTide = [arrayOfTideCycles objectAtIndex:iPlusOne]; 
       NSString *lowTidePrettyDateFormat = [upcomingLowTide valueForKeyPath:@"pretty"]; 
       lowTideTime = [self convertAndFormatDateToTimeWithString:lowTidePrettyDateFormat]; 
      } 

      upcomingTideData = [NSArray arrayWithObjects:lowTideTime, highTideTime, tideType, nil]; 
     } 
    } 

    return upcomingTideData; 
} 

我如何能清理这件事有什么建议?

+0

如果你的数组进行排序,使用二进制搜索。 –

回答

0

当我明白这个问题你不喜欢的是违反DRY在这种并行:

NSString *lowTidePrettyDateFormat = [upcomingTide valueForKeyPath:@"pretty"]; 
lowTideTime = [self convertAndFormatDateToTimeWithString:lowTidePrettyDateFormat]; 
NSDictionary *upcomingHighTide = [arrayOfTideCycles objectAtIndex:iPlusOne]; 
NSString *highTidePrettyDateFormat = [upcomingHighTide valueForKeyPath:@"pretty"]; 
highTideTime = [self convertAndFormatDateToTimeWithString:highTidePrettyDateFormat]; 

NSString *highTidePrettyDateFormat = [upcomingTide valueForKeyPath:@"pretty"]; 
highTideTime = [self convertAndFormatDateToTimeWithString:highTidePrettyDateFormat]; 
NSDictionary *upcomingLowTide = [arrayOfTideCycles objectAtIndex:iPlusOne]; 
NSString *lowTidePrettyDateFormat = [upcomingLowTide valueForKeyPath:@"pretty"]; 
lowTideTime = [self convertAndFormatDateToTimeWithString:lowTidePrettyDateFormat]; 

至于我第一眼告诉,他们是绝对相同的(除了一些不重要的局部变量名称)。因此,将它们排列成一个方法,该方法采用upcomingTide值,并返回两个潮汐时间的数组(或者这两位代码应该为您生成的数组)。

+0

好的,谢谢@matt。是的,基本上如果'''键入== Low Tide''' - >存储这个,否则使用另一个。 – Coughlin

0

几点建议:

valueForKeyPath是将运行比较慢很一般的和复杂的方法; objectForKey将会更快。

如果你的数组包含毫秒数据,那么你的代码有麻烦了。 intValue仅处理大约+/- 20亿的值。 20亿毫秒= 200万秒=有点超过一个月。使用longLongValue或doubleValue。 (如果你的数据实际上是以秒为单位的,你的代码将在2038年前后出错)。

我不知道到底是什么你正在尝试做的,但这种

NSInteger dateDelta = tideDateAsEPOCH - smallestDelta; 
    if (dateDelta < smallestDelta) { 
     smallestDelta = dateDelta; 

是错误的。只需通过调试器来浏览一下,看看这些值是如何变化的。我相信你会希望在那里有一个绝对值。

您的代码将崩溃,如果最后一个数组元素给出了最小的增量,因为你将访问数组元素的一个进一步的也就是阵列超越。

如果说万个数组元素,前5005千当前日期后,你会找到最好的元素到目前为止五千次之,每次你做显著的工作。我首先会找到最好的数组元素,然后在找到时获取所需的数据。

+0

我使用的,作为一个捕捉到通过比较MS当前的日期/时间和MS的潮汐时间找出最小的增量。我从'''smallestDelta'''的高值开始,因此每次传球都会变得更小。当最小的渔获量时,我抓住指数。 – Coughlin

+0

还更新了我的问题与阵列数据给一个例子,如果有帮助。 – Coughlin

+0

当你说:'''我会先找到最好的数组元素,那就是发现时,得到你want.'''数据 - 这就是什么即时试图做的是找到基础上,接下来的涨潮时间当前时间。但是你要说它在每个指数上都做了大量的工作。 – Coughlin