NSSet 的 NSArray 在 NSLog 中没有显示任何内容

发布于 2024-12-18 09:00:52 字数 572 浏览 1 评论 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 显示几个字符串。

我做错了什么?

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

感性 2024-12-25 09:00:52

问题在于您对 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 );
}

只有当地址(格式中的 %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:

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.

酒几许 2024-12-25 09:00:52

当您调用 intersectSet 时,我认为它是在比较指针,而不是 NSString 的内容。

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文