2009-06-23 14 views
7

Me gustaría pasar el dict al proceso del método. Pero una vez que accedo al diccionario, obtengo EXC__BAD_INSTRUCTION.¿Cómo usar el método parametrizado con NSNotificationCenter?

NSNotificationCenter *ncObserver = [NSNotificationCenter defaultCenter]; 
[ncObserver addObserver:self selector:@selector(processit:) name:@"atest" 
       object:nil]; 

NSDictionary *dict = [[NSDictionary alloc] 
          initWithObjectsAndKeys:@"testing", @"first", nil]; 
NSString *test = [dict valueForKey:@"first"]; 
NSNotificationCenter *ncSubject = [NSNotificationCenter defaultCenter]; 
[ncSubject postNotificationName:@"atest" object:self userInfo:dict]; 

En el método destinatario:

- (void) processit: (NSDictionary *)name{ 
    NSString *test = [name valueForKey:@"l"]; //EXC_BAD_INSTRUCTION occurs here 
    NSLog(@"output is %@", test); 
} 

alguna sugerencia sobre lo que estoy haciendo mal?

Respuesta

17

Recibirá un objeto NSNotification, no un NSDictionary en la devolución de llamada de notificación.

Prueba esto:

- (void) processit: (NSNotification *)note { 
    NSString *test = [[note userInfo] valueForKey:@"l"]; 
    NSLog(@"output is %@", test); 
} 
2

Amrox tiene toda la razón.

También se puede usar objetos (en lugar de userInfo) para la misma de la siguiente manera:

- (void) processit: (NSNotification *)note { 

    NSDictionary *dict = (NSDictionary*)note.object; 

    NSString *test = [dict valueForKey:@"l"]; 
    NSLog(@"output is %@", test); 
} 

En este caso, su postNotificationName: objeto se verá así:

[[NSNotificationCenter defaultCenter] postNotificationName:@"atest" object:dict]; 
+0

Gracias Adrian para actualizar el código. Me ocuparé de formatear también, desde la próxima vez. :) –

0

Recibirá un NSNotification objeto, no un NSDictionary en la devolución de llamada de notificación.

  • (void) processit: (NSNotification *) Nota {

    NSDictionary dict = (NSDictionary) note.object;

    NSString * test = [dict valueForKey: @ "l"];

    NSLog (@ "salida es% @", prueba); }

Cuestiones relacionadas