I am writing a piece of code, where I want the time for how long a button has been caught. To do this, I entered a NSDate ()
when the button was pressed, and tried to use the timeIntervalSinceDate
function when the button was issued. It seems to work, but I can not find any way to print results or switch to an integer.
spent = date (). Time ServerSign (TimeTime)
Note ()
Date
After reference Date ()
A new date object , And then returns the difference between TimeIntercins
at that time and timechanges
. It will return a Floating Point value (technically, a TimeInterval
).
If you want to shorten a int
value, then you can use it:
And, BTW, your time-to-press
does not require the definition of a variable to modify an date
It seems that you intend:
var timings start: date!
This defines the variable as the date
variable (an undisclosed form), but you can possibly postpone the actual acceleration of that code. Unless that can not be pressed
is called.
Alternatively, I often use, like,
start: CFAbsoluteTime!
And when I want to set up startTime
, I do the following:
start = CFAbsoluteTimeGetCurrent () < / Code>
And when I want to calculate the number of seconds remaining, I do the following:
expired = CFAbsoluteTimeGetCurrent () - Start
It is worth noting that the documentation warns us:
Increasingly called nonantonic calls to this function W No guarantee Inamon. System time can be reduced due to synchronization or explicit user change of the clock with external time references.
This means that if you are unfortunate, these adjustments occur, you can end up with the wrong calculation time. This is also true for NSDate
/ date
calculations. The best way to use based on mach_absolute_time
based calculation (most easily done):
start = cacrrt media time ()
< / Pre> and
passed = cacrrmm mediatime () -
launches mach_absolute_time
, But avoids some of the complications mentioned herein. / P>
Remember, however, when the device is rebooted, CACurrentMediaTime
/ will be reset to mach_absolute_time
. Therefore, the bottom line, if you are running an app, then calculate the precise last time, then use CACurrentMediaTime
but if you are going to save this initial time in constant storage, Which you may recall, after restarting the app in some future date, you will have to use the date
or CFAbsoluteTimeGetCurrent
, and just be with any impurity Which can be.
Comments
Post a Comment