Re: Creating an NSTimer subclass
Re: Creating an NSTimer subclass
- Subject: Re: Creating an NSTimer subclass
- From: leenoori <email@hidden>
- Date: Fri, 10 Nov 2006 18:54:11 +0100
El 10/11/2006, a las 11:02, Mike Abdullah escribió:
Perhaps it would help a little more if you explained what the
purpose of this timer is.
Yes, I can do that: I want to add two convenience methods, "pause"
and "resume", and I need some instance variables in order to
implement them. Pausing is done by setting a fire date in the distant
future. Resuming is done by putting the fire date back again after
adjusting it to take into account the time elapsed while paused.
Instance variables are required because the timer needs to remember
the old fire date, take into account how long it has been paused, and
adjust the old fire date accordingly.
The methods are really just convenience methods for something that
I've found myself wanting to do at various points of the years. And
each time I've done it I've ended up writing auxiliary helper methods
in the controllers that use the timers with names like, "startTimer",
"stopTimer" etc. Rather than do this yet again, I'd like to
encapsulate the behaviour inside the timer itself. This is a pretty
logical thing to want to do with a timer, I think. Another logical
thing I'd like to add to NSTimer is an "elapsedTime" method
(returning the time interval elapsed since the timer was scheduled).
Once again, just a convenience. You can write code that users timers
and doesn't use any of these proposed methods, but they all seem like
logical candidates for encapsulation within the timer object itself;
they're not the kind of methods that will revolutionize everything
they touch, but they should provide a couple of nice conveniences.
The question of pausing/resuming has come up in the past, and
possible workarounds have been suggested:
http://nslog.com/2004/11/25/nstimers_need_voidpauseidsender/
But when I search Google for "subclassing NSTimer" or "NSTimer
subclass" I haven't seen any examples at all of anyone ever
successfully subclassing NSTimer for any purpose whatsoever.
So those are the specifics, but I think that the general flavor of
the question is important: how do you create an NSTimer subclass?
There are plenty of possible reasons rather than the ones I cite
above. Fundamentally it comes down to this: why doesn't even an empty
subclass work and how can you make it work?
@interface TimerSubclass : NSTimer
@end
@implementation TimerSubclass
@end
I think it highlights a problem that often crops up in the Cocoa
frameworks: you want to augment the behaviour of a Cocoa class in an
extrememly minor way, a mere tweaking of the behaviour, but the
framework prevents you from doing the logical, simple, object-
oriented thing (making a subclass) and you instead either have to
reimplement the class from scratch or engage in some hideous
kludgery, making it less of a refined subclass and more like a
twisted mutant offspring of the parent.
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden