Re: NSTimer and call-by-reference
Re: NSTimer and call-by-reference
- Subject: Re: NSTimer and call-by-reference
- From: Colin Jackson <email@hidden>
- Date: Fri, 14 Feb 2003 08:40:53 +0000
Right OK then. Here is the full code.
The method is invoked by these:
[self timerForController:@"RedUnderline" type:@"Timeout" target:sender
selector:@selector(underLineIfRequired:) repeats:NO
timer:redlineTimer];
[self timerForController:@"SpellPanel" type:@"Timeout"
target:sender selector:@selector(showGuessPanelIfRequired:) repeats:NO
timer:panelTimer];
[self timerForController:@"SpeakPhrase" type:@"Timeout"
target:sender selector:@selector(speakIfRequired:) repeats:NO
timer:phraseTimer];
[self timerForController:@"PlaySound" type:@"Timeout"
target:sender selector:@selector(beepIfRequired:) repeats:NO
timer:soundTimer];
[self timerForController:@"RedUnderline" type:@"Every"
target:sender selector:@selector(underLineIfRequired:) repeats:YES
timer:redlineRegTimer];
[self timerForController:@"SpellPanel" type:@"Every"
target:sender selector:@selector(showGuessPanelIfRequired:) repeats:YES
timer:panelRegTimer];
[self timerForController:@"SpeakPhrase" type:@"Every"
target:sender selector:@selector(speakIfRequired:) repeats:YES
timer:phraseRegTimer];
[self timerForController:@"PlaySound" type:@"Every"
target:sender selector:@selector(beepIfRequired:) repeats:YES
timer:soundRegTimer];
And here is the full method:
- (void) timerForController:(NSString *)controller type:(NSString
*)type target:(id)target selector:(SEL)aSelector
repeats:(BOOL)doesRepeat timer:(NSTimer *)timer {
double delay;
if ([Defaults itemActive:type inController:controller]) {
delay = [Defaults getDoubleStateForKey:[[controller
stringByAppendingString:type]stringByAppendingString:@"Time"]];
if ([[Defaults getStringForKey:[[controller
stringByAppendingString:type]stringByAppendingString:@"Unit"]]
isEqualToString:@"Minutes"]) {
delay *= 60;
}
NSLog(@"about to check validity");
if ([timer isValid]) {
NSLog(@"Timer is valid");
if (!doesRepeat) {
NSLog(@"and doesn't repeat");
[timer invalidate];
timer = [NSTimer scheduledTimerWithTimeInterval:delay
target:target selector:aSelector userInfo:[controller
stringByAppendingString:type] repeats:doesRepeat];
timer = [timer retain];
}
}
else {
NSLog(@"Timer isn't valid");
NSLog([controller stringByAppendingString:type]);
timer = [NSTimer scheduledTimerWithTimeInterval:delay
target:target selector:aSelector userInfo:[controller
stringByAppendingString:type] repeats:doesRepeat];
timer = [timer retain];
}
}
else {
if ([timer isValid])
[timer invalidate];
}
}
I hope this helps.
Thanks,
Colin
On Friday, February 14, 2003, at 02:27 am, Shawn Erickson wrote:
On Thursday, February 13, 2003, at 04:44 AM, Colin Jackson wrote:
I have a method with the following signature:
- (void) timerForController:(NSString *)controller type:(NSString
*)type target:(id)target selector:(SEL)aSelector
repeats:(BOOL)doesRepeat timer:(NSTimer *)timer
For some reason, whenever I call this method, whatever timer is passed
gets duplicated. ie, the method checks that the timer valid and is
still doing the right thing according to current circumstances (I
won't
bore you with them), then recreates the timer if it is not.
When I check if the timer is valid, it always returns no. Then it
creates a new timer so I get lots of events firing. It looks like the
NSTimer isn't being called-by-reference properly.
By the way, this works when I use the same code directly (instead of
inside a method). Its just that I need to have 8 timers which do
pretty much the same thing so I don't want to have to inline code for
each of them.
Not sure we can help much with knowing what the code inside the method
looks like.
It sounds like someplace the method is making an incorrect decision
about the timer and is deciding to create a new instance when is
really shouldn't be. Possibly a retain issue or accidental allocating
a new timer when attempting to define a temporary variable?
All object communication happens by messages and it doesn't get
changed by the fact that it is inside an arbitrary method or not. Also
all objects are referenced by pointers (you cannot create an object on
the stack in Objective-C) so going across a method cannot affect this
(unless you are not passing down what you think you are passing down).
-Shawn
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.