سؤال

أود أن تمر ديكت طريقة processit.ولكن بمجرد الوصول إلى القاموس ، أحصل على EXC__سيئا_التعليمات.

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

أي اقتراحات بشأن ما أفعله خطأ ؟

هل كانت مفيدة؟

المحلول

وسوف تتلقى كائن NSNotification، وليس NSDictionary في رد الإخطار.

وجرب هذا:

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

نصائح أخرى

وAmrox هو على حق تماما.

واحد ويمكن أيضا استخدام الكائنات (بدلا من المعلومات حول المستخدم) لنفسه على النحو التالي:

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

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

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

في هذه الحالة الخاصة بك postNotificationName: الكائن سيبدو:

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

سوف تتلقى NSNotification كائن لا NSDictionary في الإخطار رد.

  • (الفراغ) processit:(NSNotification *)ملاحظة {

    NSDictionary ديكت = (NSDictionary)ملاحظة.الكائن ؛

    NSString *اختبار = [ديكت valueForKey:@"l"];

    NSLog(@"إخراج % @" ، اختبار);}

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top