字典和 label.text 未捕获的异常
我尝试在字典中搜索(通过观察者传递):
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
从您发布的错误(但没有看到更多代码,您应该也在那里发布)来看,您似乎正在尝试在
objectForKey
上执行objectForKey
方法code>ThirdViewController 对象(这是一个视图控制器),而不是您想要从中获取对象的实际NSDictionary
。当您调用
objectForKey
时,请确保dictionnary
设置正确。这样做:
而不是这样做:
我链接了苹果的 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 yourThirdViewController
object (which is a view controller), not the actualNSDictionary
you wanted to get your object from.Make sure
dictionnary
is set properly when you are callingobjectForKey
on it.Do this:
instead of this:
I've linked Apple's NSDictionary
userInfo
documentation for you.