I am trying to make this code work, the idea is to compare two arrays of strings, first is names and second is words, so I try to find the word from first array, then compare it to all words in second array, and if I get positive result, I print the word out. But it doesn’t work as intended, it just prints all the array of names. What’s wrong with this code? Maybe there is a more effective way to do this?
for (NSString *n in names) {
for (NSString *m in words) {
if ([n caseInsensitiveCompare:m] == NSOrderedSame) {
NSLog(@"%@", n);
}
}
}
I tried another way and it just keeps printing names one after another. Tried swapping words for names for same result. Something is wrong with comparing to each other.
for (NSString *n in names) {
NSString *newN = [n lowercaseString];
for (NSString *m in words) {
NSString *newM = [m lowercaseString];
if ([newN isEqualToString: newM]) {
NSLog(@"%@ is equal to %@", newN, newM );
}
}
}
This thing provides same results! Duh.
NSArray *names = [nameString componentsSeparatedByString:@"\n"];
NSArray *words = [itemsString componentsSeparatedByString:@"\n"];
int i = 0;
int j = 0;
while (i != [names count] ) {
while (j != [words count]) {
if ([[names objectAtIndex:i] caseInsensitiveCompare:[words objectAtIndex:j]] == NSOrderedSame)
{
NSLog(@"Voila! Name is : %@", [names objectAtIndex:i]);
}
j++;
}
j = 0;
i++;
What is wrong? I can’t figure out, I tried. If you pick words one by one from either array, you get correct names and words. Words array does not have most names in it as the output I get. I get just names in order, Aaron, Aasomething, etc, they are not in words array.
Actually I have found an answer. The words file contains all the names from the names file. So you just get all the names. All three code variants work as intended.
Well, at least I learned something new today.