1

1 つのボタンで時間をキャプチャするタイムスタンプ アプリを作成しようとしています。

ボタンを押すと、現在の時刻が出力されます00:00:00。ボタンを 2 回押すと、現在の時刻が再度表示されます (例: 00:00:15)。

次に、2 つのタイムスタンプの時間差 (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"
    }
}

助けてくれてありがとう!

4

1 に答える 1

0

NSDate2 つのオブジェクトの違いは、[NSDate timeIntervalSinceDate:]メソッド ( referenceNSTimeInterval ) を使用して取得され、a (a typedef'd double)として表されます。

NSTimeInterval difference = [self.checkOutStamp timeIntervalSinceDate:self.checkInStamp];
NSLog(@"Difference is %f seconds", difference);
于 2013-02-14T15:04:53.863 に答える