2012-01-09 35 views
0

我嘗試在dictionnary(通過槽觀察員)搜索:與dictionnary和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」,是不是?

回答

0

從您發佈的錯誤(但沒有看到更多的代碼,你應該後出現的),看來你正在嘗試做你的ThirdViewController對象的objectForKey方法(這是一個視圖控制器) ,而不是實際的NSDictionary你想從你的對象。

確定dictionnary設置正確,當你打電話objectForKey就可以了。

這樣做:

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

,而不是這樣的:

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

我已經聯繫蘋果公司的NSDictionary userInfo文檔你。

+0

OK THAKS,事實上,我必須在一個觀察者中傳遞多個值,所以我在ThirdViewController類中做了一個字典(正如你所說的),然後通過Observer傳遞它。我抓住了它,我做了objectforkey(請檢查最初的帖子) – clement 2012-01-09 14:32:33

+0

是的,就像我預期的那樣..你分配的是錯誤的。我正在爲你更新我的答案。 – 2012-01-09 14:34:52

+0

再次感謝,但我不明白它如何幫助我。使用你的代碼我已經獲得了有關應用程序的信息,但是我需要從另一個類的myDictionnary中獲取激活的值: -/ – clement 2012-01-09 14:53:39