I want to add objects from NSArray *small whenever they don´t exist in NSArray *big.
But the output shows that the objects from *small which exist in *big are added. I have tried x isEqualToString: x == NO (I know it is not same as range.location and hasPrefix, but it is weird that even hiya -> is added when it does exist in *big)
and range.location = NSNotFound and x hasPrefix: x == NO. But neither of them does work. Why?
By the way, *small contains fewer objects than *big. Does it matter?
The codes below:
NSArray *big = [[NSArray alloc] initWithObjects:@"hello ->hi", @"hiya ->", @"hiya ->whatever", @"hiya -> howdy", @"good day ->hello", @"nope, but ->no", @"however ->what", @"May ->april", @"mai ->", nil];
NSArray *small = [[NSArray alloc] initWithObjects: @"match", @"hiya ->",@"hiya ->", @"hiya ->",@"nope, but ->", @"however ->", @"May ->", nil];
NSString *same;
NSMutableArray *newWords = [[NSMutableArray alloc]init];
newWords = [NSMutableArray arrayWithArray: big];
NSLog (@"big: %@", big);
int i;
for (i = 0; i<[small count]; i++)
{
same = [small objectAtIndex:i];
for (NSString *s in big)
{
//NSRange ran = [s rangeOfString:same];
//if (ran.location =NSNotFound)
//if ([s isEqualToString: same] == NO)
if ([s hasPrefix:same] == NO)
{
[newWords addObject:same];
break;
}
}
}
The output shows:
2011-10-17 19:21:56.855 scanner2[4018:207] newWords: (
"hello ->hi",
"hiya ->",
"hiya ->whatever",
"hiya -> howdy",
"good day ->hello",
"nope, but ->no",
"however ->what",
"May ->april",
"mai ->",
match,
"hiya ->",
"hiya ->",
"hiya ->",
"nope, but ->",
"however ->",
"May ->"
)
edit: I even tried if ([x compare: x ] !=NSOrderedSame), but only hiya -> is added thrice to *newWords.
You are comparing each string in Small to each string in Big. If ANY of the strings in Big are not the same as the one you’re comparing, then you’re adding it to Big. But actually, you want to add it if ALL of the strings are not the same. So, you need to wait until after checking the whole big array.
Try this instead:
To be pedantic, I should add that if the arrays are going to be MUCH bigger than the demo strings you’re showing, then you might want to maintain Big as a sorted array, so that you can find presence or absence much faster.
For example:
Obviously if you’re doing this multiple times, just keep big as a sorted mutable array, rather than re-sorting it each time.