2013-02-14 221 views
1

我試圖做一個時間戳應用程序,您可以通過一個按鈕捕捉時間。時間戳。兩次之間的差異

當按鈕被按下時,它打印出當前時間00:00:00。 當第二次按下按鈕時,它再次打印當前時間(即00:00:15)。

然後我希望它計算兩個時間戳(10秒)之間的時間差。

我是新來的Objective-C,我一直在坐着這個小小的問題幾個小時。在正確的方向上有一個指針會很好。

我遇到了問題timeIntervalSinceDate。這裏是代碼:

- (IBAction)checkButton:(id)sender { 


if (buttonPress) { 

    self.checkInStamp = [NSDate date]; 

    NSDateFormatter *timeFormatter = [[NSDateFormatter alloc] init]; 
    [timeFormatter setTimeStyle:NSDateFormatterMediumStyle]; 

    checkInTime.text = [timeFormatter stringFromDate:checkInStamp]; 
    buttonPress = false; 
} 
    else { 

     self.checkOutStamp = [NSDate date]; 

     NSDateFormatter *timeFormatter = [[NSDateFormatter alloc] init]; 
     [timeFormatter setTimeStyle:NSDateFormatterMediumStyle]; 

     checkOutTime.text = [timeFormatter stringFromDate:checkOutStamp]; 

     NSTimeInterval timeDifference = [checkOutStamp timeIntervalSinceDate:checkInStamp]; 

     totalDuration.text = @"%d", timeDifference; //<-This gives "Expression result unused on build" 
    } 
} 

謝謝任何​​幫助!

回答

0

2個NSDate對象使用[NSDate timeIntervalSinceDate:]方法(reference)獲得並作爲NSTimeInterval(一個typedef倒是double)表示之間的差異:

NSTimeInterval difference = [self.checkOutStamp timeIntervalSinceDate:self.checkInStamp]; 
NSLog(@"Difference is %f seconds", difference);