NSSet 的 NSArray 在 NSLog 中没有显示任何内容
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 显示几个字符串。
我做错了什么?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
问题在于您对 intersectSet 工作原理的理解。
我认为您期望它比较 newsmall 和 newnewbig 的字符串内容,但它真正做的是比较对象地址。
在执行
intersectSet
调用之前执行此操作:只有当地址(格式中的
%p
)匹配时,intersectSet
才会起作用。字符串内容可能匹配,但 intersectSet 关心的是字符串地址。所以实际上,您的解决方案是您需要采用不同的方式来比较集合之间的字符串。
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: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.
当您调用 intersectSet 时,我认为它是在比较指针,而不是 NSString 的内容。
看看这里,它可能会有所帮助:SO问题
When you call
intersectSet
, I think it's comparing pointers, and not the content of yourNSString
.Have a look here, it may help : SO Question