Pregunta

Me gustaría pasar dict al método processit. Pero una vez que accedo al diccionario, obtengo EXC__BAD_INSTRUCTION.

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 del 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?

¿Fue útil?

Solución

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

Otros consejos

Amrox tiene toda la razón.

También se puede usar Object (en lugar de userInfo) para lo mismo que a continuación:

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

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

  • (nulo) proceso: (NSNotification *) nota {

    NSDictionary dict = (NSDictionary ) note.object;

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

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

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top