2010-02-18 80 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

相关问题