带有 NSStrings containstObject 的 NSSet 在应该返回 YES 时却没有返回 YES

发布于 2024-11-29 09:02:30 字数 730 浏览 4 评论 0原文

我正在将字典(单词列表,而不是类)作为 NSString 加载到 NSSet 中。然后,我反复向该集发送消息 -containsObject:someNSString。但它总是返回 false。我编写了一些代码来测试它:

NSLog(@"Random from dictionary: %@", [dictionary anyObject]);
NSString *test = [NSString stringWithFormat:@"BEMIRED"];
NSLog(@"To match this word: %@", test);
if ([dictionary containsObject:test])
    NSLog(@"YES!");

在日志中我得到以下内容:(

Random from dictionary: BEMIRED
To match this word: BEMIRED

我错过了“是!”)

当我尝试使用 CFShow(dictionary) 时,我可以看到它实际上包含字符串和所有内容。举个例子:

0 : <CFString 0xc3bd810 [0x1386400]>{contents = "BEMIRED"}
3 : <CFString 0xdf96ef0 [0x1386400]>{contents = "SUBJECTIFIED"}

有人可以帮我吗? 谢谢!

I'm loading a dictionary (list of word, not the class) into a NSSet as NSStrings. I then repeatedly send this set the message -containsObject:someNSString. But it always returns false. I wrote some code to test it:

NSLog(@"Random from dictionary: %@", [dictionary anyObject]);
NSString *test = [NSString stringWithFormat:@"BEMIRED"];
NSLog(@"To match this word: %@", test);
if ([dictionary containsObject:test])
    NSLog(@"YES!");

In the log I get the following:

Random from dictionary: BEMIRED
To match this word: BEMIRED

(I'm missing the "YES!")

When I try using CFShow(dictionary) I can see that it actually contains Strings and that everything. An example:

0 : <CFString 0xc3bd810 [0x1386400]>{contents = "BEMIRED"}
3 : <CFString 0xdf96ef0 [0x1386400]>{contents = "SUBJECTIFIED"}

Can anyone please help me here?
Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

最好是你 2024-12-06 09:02:30

NSSet 使用 isEqual: 来测试对象相等性,NSString 会重写它以按照您的预期执行字符串比较。以下单元测试通过:

- (void)testSetStrings
{
    NSSet *set = [NSSet setWithObject:@"String 1"];

    // I've used the UTF8 initializer to avoid any cleverness from reusing objects
    NSString *string1 = [[[NSString alloc] initWithUTF8String:"String 1"] autorelease];

    // Test the references/pointers are not the same
    STAssertTrue([set anyObject] != string1, nil);

    STAssertTrue([set containsObject:string1], nil);
}

我们可以看到两个字符串具有不同的指针值,但该集合仍然为 containsObject: 调用返回 YES。

所以我猜你的字符串实际上并不相等。我会检查隐藏的空格或其他类似的问题。

NSSet uses isEqual: to test for object equality, which NSString overrides to perform a string comparison as you would expect. The follow unit test passes:

- (void)testSetStrings
{
    NSSet *set = [NSSet setWithObject:@"String 1"];

    // I've used the UTF8 initializer to avoid any cleverness from reusing objects
    NSString *string1 = [[[NSString alloc] initWithUTF8String:"String 1"] autorelease];

    // Test the references/pointers are not the same
    STAssertTrue([set anyObject] != string1, nil);

    STAssertTrue([set containsObject:string1], nil);
}

We can see the two strings have different pointer values, but the set still returns YES for the containsObject: call.

So I would guess your strings are not in fact equal. I would check for hidden whitespace or other similar issues.

萌能量女王 2024-12-06 09:02:30

-[NSSet containsObject:] 似乎仅检查指针值(文档是非常缺乏该方法),而不是对象平等。所以你需要使用 -[NSSet member:] 代替,它使用 isEqual: 来检查被认为相等的对象是否在您的集合中。

if ([dictionary member:test])
    NSLog(@"YES!");

Edit: 实际上,containsObject: 似乎确实使用了<代码> isEqual:也是如此。它们似乎仅在返回内容上有所不同(containsObject: 返回 BOOLmember: 返回 id)。我保留这个答案用于文档目的。

The -[NSSet containsObject:] seems to check for the pointer value only (the documentation is very lacking for that method), not for object equality. So you need to use -[NSSet member:] instead, which uses isEqual: to check whether an object that is considered to be equal is in your set.

if ([dictionary member:test])
    NSLog(@"YES!");

Edit: Actually it seems that containsObject: does use isEqual: as well. They only seem to differ in what they return (containsObject: returns a BOOL while member: returns id). I'm letting this answer stay for documentation purposes.

物价感观 2024-12-06 09:02:30

好的,我解决了这个问题,它与 containsObject 方法无关。正如我评论的那样,我使用了在这里找到的 Dave DeLongs DDFileReader: Dave DeLongs DDFileReader

因此,通过在整个字典上使用 CFShow,我注意到每个单词末尾都有一个新行。因此,我使用 -readTrimmedLine (上述文件读取器中的机器人方法)代替 -readLine 方法。这为我解决了问题。

对于未来的论坛访问者,我想提请大家注意 DarkDust 和 zoul 关于 -containsObject 和 -member(NSSet 的两种方法)的讨论,事实证明它们都使用 -isEqual 方法。

Ok so I solved the problem and it had nothing to do with the containsObject method. As I commented i used Dave DeLongs DDFileReader found here: Dave DeLongs DDFileReader

So by using CFShow on the entire dictionary I noticed that every word had a new line at the end of it. So instead of the -readLine method i used the -readTrimmedLine (bot methods in above mentioned file reader). This solved the problem for me.

For future forum visitors I'd like to draw attention to the discussion DarkDust and zoul had about -containsObject and -member (both methods of NSSet) which it turns out both uses the -isEqual method.

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