views:

277

answers:

4
A: 

Your call is right, but you don't have to cast the dictionary to id. You can get the userInfo back with the following line in your notifyPause1: method:

- (void)notifyPause1:(NSTimer *)timer {

    NSDictionary *dict = [timer userInfo];

}
schaechtele
+2  A: 

sound and selector aren't Objective-C objects: sound is an unsigned number and selector is a pointer to a C struct . That's likely to cause a crash of some sort.

You'll want to use NSValue to hold the value for selector and NSNumber to hold the value for sound. NSValue and NSNumber are objects and will work with the NSMutableDictionary.

Giao
A: 

You should not cast your objects to id when assigning to the dictionary. Any object that can be embedded directly into a NSDictionary already derives from NSObject and are implicitly concidered cast to id. Store the name of the selector as an NSString (using NSStringFromSelector()) and then convert it back to a selector using NSSelectorFromString()

Claus

Claus Broch
A: 

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];
}

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"]);

    [dict release];

    // Do whatever...
}

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

Laurent Etiemble