2010-10-27 19 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

相關問題