Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

loop through array of dictionaries to find a value

I have an array of dictionaries that I would like to go through to find a matching value that I can then Count == Nth item of the array

Each item of the array looks like this

HMOD = 0;
MID = 39;
MOD = SOMETHING; // looking for this value
ID = 50;

So I would like to create a loop that goes through the array until it finds the matching value, then I use the number in the count as an reference to Index path in the next view..

I have written this peice of code which dosnt work... but hopefully it gives you an idea of the loop I am trying to create.

int count = 0;
while (singleName != [[ModArray valueForKey:@"MOD"] objectAtIndex:count]) {
                    count ++;
                    NSLog(@"%i", count);
                }

SingleName is a NSString that I am using to match the MOD value in ModArray... Any help would be greatly appreciated.

like image 898
HurkNburkS Avatar asked Dec 07 '22 11:12

HurkNburkS


2 Answers

Here is a simpler solution by using valueForKey on the array of dictionaries,

Assuming that your modArray is like this,

NSArray *modArray = [NSArray arrayWithObjects:[NSDictionary dictionaryWithObject:@"0" forKey:@"HMOD"],
                       [NSDictionary dictionaryWithObject:@"39" forKey:@"MID"],
                       [NSDictionary dictionaryWithObject:@"something" forKey:@"MOD"],
                       [NSDictionary dictionaryWithObject:@"50" forKey:@"ID"], nil];

And singleName has a value as "something"

    NSString *singleName = @"something";

Fetch the array from modArray which has an object with key as "MOD",

    NSArray *array = [modArray valueForKey:@"MOD"];

Check if singleName is present in this array. If yes, then get the first index of that object which will be same as the index of dictionary with key "MOD" in modArray.

    if ([array containsObject:singleName]) {
        NSLog(@"%d", [array indexOfObject:singleName]);
    } else {
        NSLog(@"%@ is not present in the array", singleName);
    }

Update: If you want to do it in your way, only mistake was you were using != whereas you should have used isEqualToString. You should have done like this,

int count = 0;
while (![singleName isEqualToString:[[modArray valueForKey:@"MOD"] objectAtIndex:count]]) {
                    count ++;
                    NSLog(@"%i", count);
                }
like image 163
iDev Avatar answered Jan 04 '23 19:01

iDev


Your code looks all inside out. You state you have an array of dictionaries. Assuming ModArray is the array (based on the name) you might do this:

NSUInteger count = 0;
for (NSDictionary *dict in ModArray) { // iterate through the array
    NSString *mod = dict[@"MOD"];      // get the value for MOD
    if ([mod isEqualToString:singleName]) {  // compare the two strings
        break;                         // they match so exit the loop
    }
    count++
}

// count has the index of the dictionary with the matching MOD value

Edit: Based on ACB correcting my misunderstanding of NSArray valueForKey:, the only real issue is your use of using != to compare the two strings.

like image 42
rmaddy Avatar answered Jan 04 '23 20:01

rmaddy