iPhone timeIntervalSinceDate 不抛出错误,或者工作

发布于 2024-09-28 23:03:18 字数 1191 浏览 0 评论 0原文

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

    //----------- 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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

太阳哥哥 2024-10-05 23:03:18

编辑:查看您添加的额外代码,您没有保留 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 :)

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文