Re: NSTimer, and NSTimeInterval help?
Re: NSTimer, and NSTimeInterval help?
- Subject: Re: NSTimer, and NSTimeInterval help?
- From: Bill Bumgarner <email@hidden>
- Date: Sat, 19 Jul 2008 12:50:22 -0700
On Jul 19, 2008, at 12:41 PM, Eric Lee wrote:
I'm planning on making a stopwatch where the timer fires and then
the text field updates by each second.
I read online that I should use NSTimeInterval for this, as well as
NSDate, and NSTimer.
My main question is, how do you use NSTimeInterval?
NSTimeInterval isn't an object, it is a scalar type. Specifically:
typedef double NSTimeInterval;
A double; integral part is # of seconds and fractional part is # of
nths of a second.
Typically, you would create a start time:
NSTimeInterval startTime = [NSDate timeIntervalSinceReferenceDate];
And then grab an endTime when you are done:
NSTimeInterval endTime = [NSDate timeIntervalSinceReferenceDate];
And calculate an elapsed time:
NSTimeInterval elapsedTime = endTime - startTime;
Obviously, these variables should be instance variables or otherwise
persisted beyond a local scope.
In any case, you use the time intervals and -
timeIntervalSinceReferenceDate to keep track of the actual elapsed
time. The NSTimer is used to update UI. You don't want to do any
interval calculations based when the timer fires as timers are
inaccurate. However, you can grab the elapsed time in the timer's
fired method via -timeIntervalSinceReferenceDate and calculate based
upon that.
b.bum
Attachment:
smime.p7s
Description: S/MIME cryptographic signature
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden