2010-02-18 109 views
19

我保持NSMutableDictionary pair.Now我需要爲每個it.How值從字典retrive值執行一些操作保持鍵和值。如何獲取Objective-C中字典中每個鍵的值?

// this is NSMutableDIctionary 
NSMutableDictionary *dictobj = [[NSMutableDictionary alloc]init]; 
// in methodA 

-(void)methodA 
{ 
    //get the value for each key and peform an operation on it. 
} 

如何進行? plz幫助

回答

43
for (id key in dictobj) 
{ 
    id value = [dictobj objectForKey:key]; 
    [value doSomething]; 
} 
6

我不是完全清楚你的問題是問什麼,但你可能會尋找:

for (id currentValue in [dictobj allValues]) 
{ 
    //stuff with currentValue 
} 
+0

wher是CurrentValue的聲明?其中的類對象是什麼? – suse 2010-02-18 04:04:23

+1

這是一個利用「快速列舉」(就像其他的答案)的。 'currentValue'在for-in循環中聲明爲正確。 – andyvn22 2010-02-18 04:13:22

0
NSArray *array = [viewControllers allValues]; 
for (int i = 0; i<array.count; i++) {   
    MenuListingVC *vc = array[i]; 
    [vc tableDataReload]; 
} 
+1

添加一些解釋,使答案更容易理解。 – rptwsthi 2016-01-27 12:53:28

相關問題