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 和 newnewbig 有一些匹配的字符串,所以我希望 arrayfour 显示几个字符串。

我做错了什么?

4

2 回答 2

2

当你打电话时intersectSet,我认为这是比较指针,而不是你的内容NSString

看看这里,它可能会有所帮助:SO Question

于 2011-11-28T10:54:27.863 回答
2

问题在于您对intersectSet工作原理的理解。

我认为您期望它比较来自 newsmall 和 newnewbig 的字符串的内容,但它真正做的是比较对象地址。

在拨打电话之前执行此操作intersectSet

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仅当地址(%p在那里的格式中)匹配时才有效。字符串内容可能匹配,但 intersectSet 关心的是字符串地址。

所以真的,您的解决方案是您需要以不同的方式比较集合之间的字符串。

于 2011-11-28T10:55:02.503 回答