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

发布于 2024-12-25 20:52:05 字数 3219 浏览 0 评论 0原文

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

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

观察者是从“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”,不是吗?

I try to search in the dictionnary (passed trough an observer):

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

... to set the text in a label :

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

But it make me an uncaught exception :

-[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

Thanks to help me, I don't understand my mistake

EDIT:

Here is the whole method :

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

And the Observer is send from "ThirdViewController" class :

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

Thanks again !!!

EDIT 2:

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

Give me

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)

and

text: 

This should give me @"recapitulatif de l'installe", isn't it ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

风尘浪孓 2025-01-01 20:52:05

从您发布的错误(但没有看到更多代码,您应该在那里发布)来看,您似乎正在尝试在 objectForKey 上执行 objectForKey 方法code>ThirdViewController 对象(这是一个视图控制器),而不是您想要从中获取对象的实际 NSDictionary

当您调用 objectForKey 时,请确保 dictionnary 设置正确。

这样做:

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

而不是这样做:

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

我链接了苹果的 NSDictionary userInfo 文档。

From the error you posted (but not seeing more of your code, which you should also post there), it appears that you are trying to do the objectForKey method on your ThirdViewController object (which is a view controller), not the actual NSDictionary you wanted to get your object from.

Make sure dictionnary is set properly when you are calling objectForKey on it.

Do this:

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

instead of this:

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

I've linked Apple's NSDictionary userInfo documentation for you.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文