Compare and fetch objects stored in two NSDictionary

Go To StackoverFlow.com

0

How can i fetch objects that are not in first NSDictionary , but are in second ? Below approach give me same object several times

  for (NSString *numCodetmp in numCodeForInsertingRow) {
                for (NSString *numCodeInsertmp in numCode) 
                   if (![numCodetmp  isEqualToString:numCodeInsertmp])
                       NSLog(@"Equal : %@, %@ ",numCodetmp,numCodeInsertmp);
            }
2012-04-04 16:53
by NoName


1

So, realize that when you nest for loops like you did, the outter for loop gets executed once for every complete iteration of the inner for loop. Second of all, your if statement checks for the opposite of what the NSLog statement says. Your if statement compares numCodeTmp to numCodeInsertmp and returns a bool stating whether they are equal or not. You then invert that result and check if that evaluates to true. Thus, you're saying if the two are NOT equal (which will evaluate to true...confusing, I know), print Equal : %@, %@. I assume you meant to remove the "!".

Ok, now for the bigger issue. If you're comparing objects in a dictionary, you have to give it a key by which to access that particular object. I can't tell what numCodeForInsertingRow and numCode are from the code you provided above, but if they are dictionaries, you're not grabbing the objects out of them correctly. If you're just comparing strings, comparing NSSets would be an easier way to check for differences. If the strings are keys for objects in a dictionary, and THAT is what you're comparing, you need to either

  1. Create an array of keys using [dict allKeys] and then run your for loops that way. This assumes that if Obj1 exists in Dict1 and Dict2, Key1 is the same in both.
  2. Create an array of objects for a particular key for each dictionary. If [dict1Array count] > 0 && [dict2Array count] > 0, the object exists in both dictionaries

There are a number of other ways to compare, but the NSSet and the two points above are just a few examples.

2012-04-04 17:17
by jmstone617


0

Perhaps you can use the keysOfEntriesPassingTest: method? Something like:

NSSet *keysInDictTwoOnly;
keysInDictTwoOnly = [secondDict keysOfEntriesPassingTest:^(id key, id obj, BOOL *stop){
    return ([firstDict objectForKey:key] != obj);
}];

I.e., a key is in keysInDictTwoOnly if either it is not in firstDict or else the object in firstDict under this key is not the same as the object in secondDict under this key.

2012-04-04 17:28
by Glenn


0

Use the allKeys method to get all the keys from the second dictionary.

Use a

for (object in dictionary) 

Style of for loop to loop through those keys.

Use each key in turn to see if there is an object with that key in the other dictionary. (using objectForKey). If a given key does not exist in the other dictionary, that's one of the items you are looking for.

The other poster's idea of using the keysOfEntriesPassingTest method would probably work too, but that might be a little confusing to implement if you're not comfortable with blocks.

2012-04-04 20:44
by Duncan C


0

I would do it like this:

NSArray *keys = [dictionary allKeys];

for (int i = 0; i < keys.count; i++)
{
    for (int j = i + 1; j < keys.count; j++)
    {
         if ([[keys objectAtIndex:i] isEqual:[keysObjectAtIndex:j]])
         {
             // insert code here
         }
    }
}

This has the advantage of not comparing keys to each other and not comparing multiple times, as well as increased performance.

2012-04-04 20:54
by Richard J. Ross III


0

// assuming that you have NSMutableDictionary objects, and the key values are PKs you can use for comparison:

for (NSString *firstSetKey in firstSet)
{
     [secondSet removeOjbectForKey:firstSetKey];
} 
2012-11-09 00:35
by software evolved
Ads