Search code examples
iosobjective-cnsarray

How to create an NSArray of times with 5mins interval between objects?


I am stuck trying to get an array of time fetched from a website. I got the array which happens to be this one:

NSArray *rawData = @[@"8:06", @"8:07", @"8:08", @"8:09", @"8:10", @"8:11", @"8:12", @"8:13",@"8:14", @"8:15", @"8:16", @"8:17", @"8:18", @"8:19", @"8:20", .... @"15:05"];

So the array goes from 8:06 up to 15:05 in 1min increments. What I want I just can't figure out how to do, is a new array from that one but starting with the first 5 multiple time, i.e: 8:10 and the go on incrementing by 5 mins up to 15:05, so that the NSLog of the final array would be like this:

8:10, 8:15, 8:20, 8:25, ...., 10:45, 10:50, 10:55, 11:00, 11:05, ...., 14:55, 15:00, 15:05

So any ideas???


Solution

  • In this particular case you could just check if the time string ends with "0" or "5":

    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF ENDSWITH '0' OR SELF ENDSWITH '5'"];
    NSArray *filteredData = [rawData filteredArrayUsingPredicate:predicate];
    

    (Note that this is a special solution for multiples of 5. You could proceed similarly for multiples of 10, but it would not work in other cases like multiples of 3 or 7.)