带有字典和label.text的未捕获的异常

时间:2012-01-09 14:11:39

标签: iphone objective-c xcode nsdictionary

我尝试在词典中搜索(通过观察者传递):

NSDictionary *myDictionnary = [NSDictionary dictionaryWithObjectsAndKeys:@"details",@"details de l'installation",@"recapitulatif",@"recapitulatif de l'installe",nil];

...在标签中设置文字:

NSLog(@"key: %@, value: %@", recapitulatif, [dictionnary objectForKey:@"recapitulatif"]);
details.text = [dictionnary objectForKey:@"recapitulatif"];

但它让我成为一个未被捕获的例外:

-[ThirdViewController objectForKey:]: unrecognized selector sent to instance 0x4b59c30
2012-01-09 15:07:13.179 emars[8185:b303] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[ThirdViewController objectForKey:]: unrecognized selector sent to instance 0x4b59c30'
*** Call stack at first throw:
(
    0   CoreFoundation                      0x00fb45a9 __exceptionPreprocess + 185
    1   libobjc.A.dylib                     0x01108313 objc_exception_throw + 44
    2   CoreFoundation                      0x00fb60bb -[NSObject(NSObject) doesNotRecognizeSelector:] + 187
    3   CoreFoundation                      0x00f25966 ___forwarding___ + 966
    4   CoreFoundation                      0x00f25522 _CF_forwarding_prep_0 + 50
    5   emars                               0x0000439d -[FirstViewController updateLabel:] + 134
    6   Foundation                          0x0002f669 _nsnote_callback + 145
    7   CoreFoundation                      0x00f8c9f9 __CFXNotificationPost_old + 745
    8   CoreFoundation                      0x00f0b93a _CFXNotificationPostNotification + 186
    9   Foundation                          0x0002520e -[NSNotificationCenter postNotificationName:object:userInfo:] + 134
    10  emars                               0x0000880b -[ThirdViewController connectionDidFinishLoading:] + 541

感谢帮助我,我不明白我的错误

修改

以下是整个方法:

- (void)updateLabel:(NSNotification*)notification
{
    NSDictionary *dictionnary = (NSDictionary*)[notification object];   
    NSLog(@"key: %@, value: %@", recapitulatif, [dictionnary objectForKey:@"recapitulatif"]);
}

Observer从“ThirdViewController”类发送:

NSDictionary *myDictionnary = [NSDictionary dictionaryWithObjectsAndKeys:@"details",@"details de l'installation",@"recapitulatif",@"recapitulatif de l'installe",nil];
[[NSNotificationCenter defaultCenter] postNotificationName:@"LABELUPDATENOTIFICATION" object:self userInfo:myDictionnary];

再次感谢!!!

编辑2:

- (void)updateLabel:(NSNotification*)notification
{
    NSDictionary *dictionnary = (NSDictionary*)[notification userInfo];
    NSLog(@"key: %@, value: %@", recapitulatif, [dictionnary objectForKey:@"recapitulatif"]);   
    NSLog(@"text: ", [dictionnary objectForKey:@"recapitulatif"]);
}

给我

key: <UILabel: 0x4b2ff80; frame = (20 245; 280 133); text = 'Index du Compteur: 1 572,...'; clipsToBounds = YES; opaque = NO; autoresize = RM+BM; userInteractionEnabled = NO; layer = <CALayer: 0x4b30040>>, value: (null)

text: 

这应该给我@“recapitulatif de l'installe”,不是吗?

1 个答案:

答案 0 :(得分:0)

根据您发布的错误(但没有看到您应该发布的更多代码),您似乎正在尝试在objectForKey上执行ThirdViewController方法{1}}对象(它是一个视图控制器),而不是您想要获取对象的实际NSDictionary

确保在呼叫dictionnary时正确设置了objectForKey

这样做:

NSDictionary *dictionnary = (NSDictionary*)[notification userInfo];  

而不是:

NSDictionary *dictionnary = (NSDictionary*)[notification object];   

我已经为您链接了Apple的NSDictionary userInfo文档。