Search code examples
iphonedatetimenstimeinterval

iPhone timeIntervalSinceDate not throwing error, or working


I'm trying to find the difference between two NSDates. This worked once and printed the difference, but has never worked again. I don't remember changing any thing after the one time it worked. Any ideas? Oh, and it doesn't throw an error, and if I comment out this snippet everything works.

    //----------- Touches Begin
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    touchBegins = [NSDate date];
    NSLog (@"       Tap: %d ", tapTotal);
    NSLog (@"<=========================>");
    NSLog (@"Method: touchesBegines & Ends");
    NSLog (@"   Touch Begin: %@", touchBegins);
    // [self updateLabelsFromTouches:touches];
}


//----------- Touches End
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    touchEnds = [NSDate date];
    NSLog (@"   Touch Ends : %@", touchEnds );
    @try {
        NSLog(@"%@", touchEnds);
        NSTimeInterval elapsed = [touchEnds timeIntervalSinceDate:touchBegins];
    NSLog (@"   Finger Down:  %f", elapsed);
    } @catch (NSException *ex) {}

    NSLog (@" ");

    [self updateLabelsFromTouches:touches];
}

Console:

  [Session started at 2010-10-27 10:27:18 -0400.]
       Tap: 0 
 <=========================>
 Method: touchesBegines & Ends
 Touch Begin: 2010-10-27 14:27:22 GMT
 Touch Ends : 2010-10-27 14:27:22 GMT

Solution

  • EDIT: Looking at the extra code you have added, you're not retaining touchBegins. Try this :

    [[NSDate date] retain];
    

    I'm surprised it's not just crashing when you call timeIntervalSinceDate :) - in fact, it is but you're catching the exception and then ignoring it!

    You should add some exception logging in your @catch; just this should do it :

    } @catch (NSException *ex) {
        NSLog(@"Exception getting time interval : %@", ex);
    }
    

    You might see a log message that says something like 'unrecognized selector' - you'll certainly see something interesting I'll bet!


    Have a look at this : http://www.cplusplus.com/reference/clibrary/cstdio/printf/

    %d is an integer - try %f :)