比较两个小时字符串 Objective C

Compare two hour strings Objective C

我有一个包含一个小时的字符串,例如“15:15”,以及一个包含其他小时的字符串数组,例如:@["15:00","16:00","17: 00"] 我应该将单个字符串与数组字符串进行比较,以便在公交车站获得预计到达时间,我尝试了这段代码,但它不断迭代并给我数组中的最后一个更大的值,而不是第一个更大的值,因为我需要。

int i = 0;
    horaArribada = [[[objects objectAtIndex:0]objectForKey:@"Horaris"]objectAtIndex:i];
    while ([hora compare:horaArribada]) {
        i++;
        if (i >= [[[objects objectAtIndex:0]objectForKey:@"Horaris"]count]) {
            break;
        }else{
            horaArribada = [[[objects objectAtIndex:0]objectForKey:@"Horaris"]objectAtIndex:i];
        }
    }
    self.tfHoraArribada.text = horaArribada;
    }
}

其中 objects 是来自 Parse 和 hora 的查询,其中包含一个小时。

您似乎做了很多额外的工作来遍历您的数组。相反,为您的循环尝试不同的格式:

for (NSString *horaArribada in [[objects objectAtIndex:0] objectForKey:@"Horaris"]) {
  if ([hora compare:horaArribada] == NSOrderedAscending) {
    self.tfHoraArribada.text = horaArribada;
    break;
  }
}

这假设您的 Horaris 数组已经按照从小到大的顺序排序。此外,该逻辑不适用于午夜翻转,因此您可能需要考虑到这一点。