Search code examples
iphoneobjective-cios5nsmutablearraynstimeinterval

How to compare the NSTimeInterval(NSNumber Objects) inside a NSMutableArray?


I have two NSMutableArrays(arrayContainsGoodClicks and arrayContainsBadClicks). I am initializing these Arrays with some NSTimeInterval values when the relevant Good and Bad buttons are clciked. Now my requirement is, i am getting responses from a webservice call with some TimeIntervals. Now, i want compare the NSTimeInterval values that i am getting from the Webservice responses with the values that i have stored in the two arrays. Please find below the code.

question answer="t" qno="1" tin="71" title="Greet" tout="73"
question answer="t" qno="2" tin="74" title="Have Name Tag" tout="77
question answer="t" qno="3" tin="78" title="Greet" tout="83"

I am getting the response in the above mentioned format. I am parsing my file and storing the relevant details of NSTimeInterval Values. Now i want to compare the timeIntervals between 71 and 73 seconds(74 and 77,78 and 83 respectively). I have my arrays which contains all the timeInterval values at which the user has clicked the Good and Bad buttons.

    -(void)onClickOfGood{
//NSLog(@"The current playback time in good:%g",moviePlayerController.currentPlaybackTime);
currentPlaybackTime = moviePlayerController.currentPlaybackTime;
NSNumber *goodTimeIntervals = [NSNumber numberWithDouble:currentPlaybackTime];
[arrayContainsGoodClicks addObject:goodTimeIntervals];
NSLog(@"The total count of Array is: %i",[arrayContainsGoodClicks count]);
NSLog(@"The Array contains : %@",arrayContainsGoodClicks);}

-(void)onClickOfBad{
NSLog(@"The current playback time in bad: %g",moviePlayerController.currentPlaybackTime);
currentPlaybackTime = moviePlayerController.currentPlaybackTime;
NSNumber *goodTimeIntervals = [NSNumber numberWithDouble:currentPlaybackTime];
[arrayContainsBadClicks addObject:goodTimeIntervals];
NSLog(@"The total count of Array is: %i",[arrayContainsBadClicks count]);
NSLog(@"The Array contains Bad Clicks are: %@",arrayContainsBadClicks);}

My arrays contains the following sample details...

The Array containing Good Clicks are : (
"2.065027933",
"3.153256308",
"4.216946226",
"4.94465584",
"5.688772132",
"6.48904879",
"7.256447126",
"8.000711516000001",
"8.760312588",
"9.537493762",
"10.45637486",
"11.153212146",
"11.880587144",
"12.672884372",
"13.52852395"

)

The Array containing Bad Clicks are: (
"2.70485684",
"3.713548251",
"4.593316639",
"5.353822771",
"6.049754725",
"6.930190204",
"7.592959653",
"8.353438538000001",
"9.074305708000001",
"9.905327347",
"10.809726701",
"11.448938757",
"12.321753456",
"14.106061449"

)

Can someone please help me on how to compare the NSTimeIntervals in my arrays and the received responses?


Solution

  • Seems quite trivial if you have the data in the correct format. NSTimeInterval is a typedef double so you can do comparisons using basic operators

    for(NSNumber* click in arrayContainsGoodClicks) {
        NSTimeInterval clickTimeInterval = [click doubleValue];     
        if(clickTimeInterval > 71.0 && clickTimeInterval < 73.0 ) {
           ...
        }
    }