Tag: nstimeinterval

NSTimeInterval格式

我想采取我的NSTimeInterval并格式化为一个string00:00:00(小时,分钟,秒)。 做这个的最好方式是什么?

两个date之间的差异,以秒为单位

我有一个应用程序的内容显示给用户。 我现在想知道用户实际查看内容的时间。 所以在我的头文件中,我已经声明了一个 NSDate *startTime; NSDate *endTime; 然后在我看来会出现 startTime = [NSDate date]; 然后在我看来将会消失 endTime = [NSDate date]; NSTimeInterval secs = [endTime timeIntervalSinceDate:startTime]; NSLog(@"Seconds ——–> %f", secs); 但是,应用程序崩溃,有时会出现不同的错误。 有时候是内存泄漏,有时候是NSTimeInterval的问题,有时会在第二次返回内容之后崩溃。 任何想法来解决这个问题?

find使用Swift整数NSDates之间的秒差

我正在写一段代码,我想要一个button按下多长时间。 要做到这一点,我按下了button时logging了NSDate() ,并释放button时尝试使用timeIntervalSinceDate函数。 这似乎工作,但我找不到任何方式来打印结果或切换到一个整数。 var timeAtPress = NSDate() @IBAction func pressed(sender: AnyObject) { println("pressed") timeAtPress = NSDate() } @IBAction func released(sender: AnyObject) { println("released") var elapsedTime = NSDate.timeIntervalSinceDate(timeAtPress) duration = ??? } 我见过一些类似的问题,但我不知道C,所以我很难理解给出的答案。 如果有一个更有效的方法来找出button被按下多久,我愿意提供build议。 提前致谢。

在NSUserDefaults中存储NSDate的最佳方式是什么?

在NSUserDefaults中有两种存储NSDate的方法。 选项1 – setObject:forKey: // Set NSDate *myDate = [NSDate date]; [[NSUserDefaults standardUserDefaults] setObject:myDate forKey:@"myDateKey"]; // Get NSDate *myDate = (NSDate *)[[NSUserDefaults standardUserDefaults] objectForKey:@"myDateKey"]; 选项2 – timeIntervalSince1970 // Set NSDate *myDate = [NSDate date]; NSTimeInterval myDateTimeInterval = [myDate timeIntervalSince1970]; [[NSUserDefaults standardUserDefaults] setFloat:myDateTimeInterval forKey:@"myDateKey"]; // Get NSTimeInterval myDateTimeInterval = [[NSUserDefaults standardUserDefaults] floatForKey:@"myDateKey"]; NSDate *myDate = [NSDate […]

如何在iPhone上将NSTimeInterval分解为年,月,日,时,分,秒?

我有一个时间跨度多年,我希望所有的时间组件从一年下降到秒。 我的第一个想法是整数除以一秒钟的时间间隔在一年内,从运行的总秒数中减去,在一个月内除以秒,从运行总和减去,以此类推。 这看起来很复杂,我读过,当你做一些看起来很复杂的事情时,可能有一个内置的方法。 在那儿? 我把Alex的第二种方法整合到我的代码中。 这是在我的界面中由UIDatePicker调用的方法。 NSDate *now = [NSDate date]; NSDate *then = self.datePicker.date; NSTimeInterval howLong = [now timeIntervalSinceDate:then]; NSDate *date = [NSDate dateWithTimeIntervalSince1970:howLong]; NSString *dateStr = [date description]; const char *dateStrPtr = [dateStr UTF8String]; int year, month, day, hour, minute, sec; sscanf(dateStrPtr, "%d-%d-%d %d:%d:%d", &year, &month, &day, &hour, &minute, &sec); year -= 1970; […]

在swift中将double值舍入到x个小数位数

任何人都可以告诉我如何在Swift中将double值舍入到x个小数位数? 我有: var totalWorkTimeInHours = (totalWorkTime/60/60) totalWorkTime是NSTimeInterval(双)秒。 totalWorkTimeInHours将给我的时间,但它给了我这么长的精确数量,如1.543240952039 …… 当我打印totalWorkTimeInHours时,我怎样才能把它降低到1.543?

我如何在Swift中使用NSTimer?

我试过了 var timer = NSTimer() timer(timeInterval: 0.01, target: self, selector: update, userInfo: nil, repeats: false) 但是,我有一个错误说 '(timeInterval: $T1, target: ViewController, selector: () -> (), userInfo: NilType, repeats: Bool) -> $T6' is not identical to 'NSTimer'