2013-02-14 5 views
1

하나의 버튼으로 시간을 캡처하는 타임 스탬프 앱을 만들려고합니다.타임 스탬프. 차이점은 두 번입니다

버튼을 누르면 현재 시간이 00:00:00으로 인쇄됩니다. 버튼을 두 번째로 누르면 현재 시간이 다시 인쇄됩니다 (예 : 00:00:15).

다음 두 타임 스탬프 (10 초) 사이의 시간 차이를 계산하고 싶습니다.

나는 객관적으로 새로운 - 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

[NSDate timeIntervalSinceDate:] 방법 (reference)를 사용하여 수득되고 NSTimeInterval (a typedefdouble을 거라고)로 표현되는 두 NSDate 개체 차이 :

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