0
NSMutableSet *intersection = [NSMutableSet setWithArray:newsmall];

//this shows an array of newsmall as expected
NSLog(@"intersection %@", intersection);

[intersection intersectSet:[NSSet setWithArray:newnewbig]];

//this shows nothing
NSLog(@"intersection %@", intersection);

//this shows an array of newnewbig as expected
NSLog(@"newnewbig %@", newnewbig);

NSArray *arrayfour = [intersection allObjects];

//this shows nothing
NSLog(@"arrayfour %@", arrayfour);

newsmall and newnewbig have some matched strings, so I expect arrayfour to show a couple of strings.

What did I do wrong?

EmptyStack
  • 51,274
  • 23
  • 147
  • 178
wagashi
  • 894
  • 3
  • 15
  • 39

2 Answers2

2

When you call intersectSet, I think it's comparing pointers, and not the content of your NSString.

Have a look here, it may help : SO Question

Community
  • 1
  • 1
NSZombie
  • 1,857
  • 14
  • 14
2

The problem is in your understanding of how intersectSet works.

I think you are expecting it to compare contents of the strings from newsmall and newnewbig, but what it's really doing is comparing object addresses.

Do this before you do the intersectSet call:

NSUInteger index = 0;
for(NSString * aString in newsmall)
{
    NSLog( @"newsmall string %d is %p %@", index++, aString, aString );
} 

index = 0;
for(NSString * aString in newnewbig)
{
    NSLog( @"newnewbig string %d is %p %@", index++, aString, aString );
}

intersectSet will only work if the address (the %p in the formatting up there) matches. The string contents might match, but what intersectSet cares about is the string address.

So really, your solution is that you need to do a different way of comparing strings between sets.

Michael Dautermann
  • 88,797
  • 17
  • 166
  • 215