在Swift中准确测量时间以进行设备比较 [英] Measuring Time Accurately in Swift for Comparison Across Devices
问题描述
我需要能够记录反应时间,从屏幕加载或问题标签刷新到用户点击数字按钮。我没有在Apple上找到关于此的文档非常有帮助。 NSDate
不够准确,我需要至少测量到毫秒。 mach_absolute_time
似乎受到游戏设计师的青睐,因为它内部一致,但它不适用于这个应用程序,因为我需要跨设备比较数据, mach_absolute_time
是CPU相关时间。 此Apple Dev Q& A 建议使用 NanosecondsToAbsolute
和 DurationToAbsolute
但它在obj-c中,我找不到快捷的等效文档。
I need to be able to record reaction time, from when the screen loads or the question label refreshes until the user taps a number button. I'm not finding documentation from Apple on this to be very helpful. NSDate
is not accurate enough, I need to measure to milliseconds at least. mach_absolute_time
seems to be favored by game designers because it is internally consistent, but it won't work for this application because I need to compare data across devices, and mach_absolute_time
is CPU dependent time. This Apple Dev Q&A suggests using NanosecondsToAbsolute
and DurationToAbsolute
but it's in obj-c and I can't find a swift equivalent documentation.
我有没有找到 NanosecondsToAbsolute
和 DurationToAbsolute
的快速版本?还有其他一些方法可以做到这一点吗?
Is there a swift version of NanosecondsToAbsolute
and DurationToAbsolute
that I'm just not finding? Some other way to do this consistently?
这是我试图将时间添加到的代码:
Here's the code I'm trying to add the times to:
class EmotionQuestionsViewController: UIViewController{
override func viewDidLoad() {
super.viewDidLoad()
//mark "startTime" when view loads
}
@IBOutlet var questionLabel: UILabel!
var timeResultsStack = [String]()
var questionsStack = ["HAPPY", "ANXIOUS"]
var questionResultsStack = [String]()
var questionStackArrayIndex = 1
@IBAction func RecordValueFromNumericalScaleOneToSeven(sender: UIButton) {
//mark durration time as currentTime - startTime, append to timeResultsStack
let value = sender.currentTitle!
questionResultsStack.append(value)
if questionResultsStack.count < questionsStack.count{
self.questionLabel.text = "how \(questionsStack[questionStackArrayIndex]) are you right now?"
//mark startTime when label is changed
self.questionStackArrayIndex++
}
else{
self.performSegueWithIdentifier("showResults", sender: nil)
}
}
推荐答案
您可以使用NSTimeInterval来测量时间(比计时器好得多)。你只需要存储两个日期(两个时间点)并减去endTime - StartTime如下:
You can use NSTimeInterval to measure time (much better than a timer). You just need to store two dates (two points in time) and subtract endTime - StartTime as follow:
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var salesPriceSlider: UISlider!
var startTime: NSTimeInterval = 0
var endTime: NSTimeInterval = 0
override func viewDidLoad() {
super.viewDidLoad()
startTime = NSDate().timeIntervalSinceReferenceDate
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
@IBAction func stopTimeAction(sender: AnyObject) {
endTime = NSDate().timeIntervalSinceReferenceDate
println((endTime-startTime).time)
}
}
extension NSTimeInterval {
var time: String {
return String(format:"%d:%02d:%02d.%03d", Int(self/3600.0), Int(self/60.0 % 60), Int(self % 60), Int(self*1000 % 1000))
}
}
这篇关于在Swift中准确测量时间以进行设备比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!