views:

27

answers:

1

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
+1  A: 

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 :)

deanWombourne
Thanks, I'll check out the link. %f didn't seem to help, but it's a step in the right direction :)
rd42
Are you 100% sure that touchEnds is not nil? Try adding `NSLog(@"%@", touchEnds);` in there as well
deanWombourne
yeah, it's not nil, it gets set a few lines above that. I added more of the code and a console read out up above on the question. Thanks for your help.
rd42
you're not retaining touchBegins - try this : `[[NSDate date] retain]`
deanWombourne
You were totally right dean
rd42