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);
}
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
There are a number of other ways to compare, but the NSSet and the two points above are just a few examples.
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.
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.
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.
// 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];
}