Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing Data through NSTimer UserInfo

I am trying to pass data through userInfo for an NSTimer call. What is the best way to do this? I am trying to use an NSDictionary, this is simple enough when I have Objective-C objects, but what about other data? I want to do something like this, which doesn't work as is:

- (void)play:(SystemSoundID)sound target:(id)target callbackSelector:(SEL)selector {     NSLog(@"pause ipod");     [iPodController pause];     theSound = sound;      NSMutableDictionary *cb = [[NSMutableDictionary alloc] init];     [cb setObject:(id)&sound forKey:@"sound"];     [cb setObject:target forKey:@"target"];     [cb setObject:(id)&selector forKey:@"selector"];      [NSTimer scheduledTimerWithTimeInterval:0                                      target:self                                    selector:@selector(notifyPause1:)                                    userInfo:(id)cb                                     repeats:NO]; } 
like image 831
zorro2b Avatar asked Apr 16 '10 09:04

zorro2b


1 Answers

You have to wrap the information correctly into the dictionary:

- (void) play:(SystemSoundID)sound target:(id)target callbackSelector:(SEL)selector {     NSLog(@"pause ipod");     [iPodController pause];     theSound = sound;      NSMutableDictionary *cb = [[NSMutableDictionary alloc] init];     [cb setObject:[NSNumber numberWithInt:sound] forKey:@"sound"];     [cb setObject:target forKey:@"target"];     [cb setObject:NSStringFromSelector(selector) forKey:@"selector"];      [NSTimer scheduledTimerWithTimeInterval:0                                      target:self                                    selector:@selector(notifyPause1:)                                    userInfo:cb                                       repeats:NO];     [cb release];  } 

In notifyPause1:, you retrieve everything:

- (void)notifyPause1:(NSTimer *)timer {     NSDictionary *dict = [timer userInfo];      SystemSoundID sound = [[dict objectForKey:@"sound"] intValue];     id target = [dict objectForKey:@"target"];     SEL selector = NSSelectorFromString([dict objectForKey:@"selector"]);      // Do whatever... } 

As the timer is a repeating timer, you do not need the dictionary anymore, so you can release it.

like image 97
Laurent Etiemble Avatar answered Oct 03 '22 10:10

Laurent Etiemble