2010-10-27 23 views
0

我试图找到两个NSDates之间的差异。这工作了一次,并打印出差异,但从未再次工作。我不记得有一次它工作后改变了任何事情。有任何想法吗?哦,它不会抛出一个错误,如果我注释掉这个片段一切正常。iPhone timeIntervalSinceDate没有抛出错误,或工作

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

回答

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

+0

谢谢,我会检查出的链接。 %f似乎没有帮助,但它是朝着正确方向迈出的一步:) – rd42 2010-10-27 14:08:29

+0

你是否100%确定touchEnds不是零?尝试添加'NSLog(@“%@”,touchEnds);'在那里以及 – deanWombourne 2010-10-27 14:25:14

+0

是的,它不是零,它被设置在那之上几行。我添加了更多的代码和一个控制台,上面提到了这个问题。谢谢你的帮助。 – rd42 2010-10-27 14:30:03

相关问题