0

我试图找出两个 NSDates 之间的区别。这工作了一次并打印了差异,但再也没有工作过。我不记得在它起作用后改变任何东西。有任何想法吗?哦,它不会抛出错误,如果我注释掉这个片段一切正常。

    //----------- 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];
}

安慰:

  [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
4

1 回答 1

1

编辑:查看您添加的额外代码,您没有保留 touchBegins。试试这个 :

[[NSDate date] retain];

我很惊讶当你调用 timeIntervalSinceDate 时它不仅仅是崩溃:) - 事实上,它是但你正在捕获异常然后忽略它!

您应该在 @catch 中添加一些异常日志记录;应该这样做:

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

你可能会看到一条日志消息,上面写着“无法识别的选择器”——你肯定会看到一些有趣的东西,我敢打赌!


看看这个:http ://www.cplusplus.com/reference/clibrary/cstdio/printf/

%d 是一个整数 - 试试 %f :)

于 2010-10-27T13:58:31.577 回答