How to get the value for each key in dictionary in Objective-C?
Asked Answered
I

3

20

I'm maintaining a NSMutableDictionary which holds key and value pair.Now i need to perform some operation for each value in it.How to retrive value from dictionary.

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

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

How to proceed? plz help

Impenetrability answered 18/2, 2010 at 3:55 Comment(0)
O
44
for (id key in dictobj)
{
    id value = [dictobj objectForKey:key];
    [value doSomething];
}
Osy answered 18/2, 2010 at 4:7 Comment(0)
D
6

I'm not entirely clear what your question is asking, but you might be looking for:

for (id currentValue in [dictobj allValues])
{
    //stuff with currentValue
}
Doubleness answered 18/2, 2010 at 4:1 Comment(2)
wher is currentValue declared? of which class object is it?Impenetrability
This is a use of "fast enumeration" (just like the other answer). currentValue is declared right in the for-in loop.Doubleness
V
0
NSArray *array = [viewControllers allValues];
for (int i = 0; i<array.count; i++) {        
    MenuListingVC *vc = array[i];
    [vc tableDataReload];
}
Vector answered 27/1, 2016 at 6:7 Comment(1)
Add some explanation to make the answer more understandable.Ragtime

© 2022 - 2024 — McMap. All rights reserved.