質問

dictをメソッドprocessitに渡したいです。しかし、辞書にアクセスすると、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];

受信者メソッドでは:

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

間違ったことについて何か提案はありますか?

役に立ちましたか?

解決

通知コールバックでは、NSDictionaryではなくNSNotificationオブジェクトを受け取ります。

これを試してください:

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

他のヒント

Amroxは完全に正しい。

以下と同じ目的で(userInfoの代わりに)Objectを使用することもできます:

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

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

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

この場合、postNotificationName:objectは次のようになります。

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

通知コールバックでNSDictionaryではなくNSNotificationオブジェクトを受け取ります。

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

    NSDictionary dict =(NSDictionary )note.object;

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

    NSLog(@" output is%@&quot ;, test); }

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top