Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c NSSet with NSStrings containstObject不在应该时返回YES_Objective C_Nsstring_Nsset - Fatal编程技术网

Objective c NSSet with NSStrings containstObject不在应该时返回YES

Objective c NSSet with NSStrings containstObject不在应该时返回YES,objective-c,nsstring,nsset,Objective C,Nsstring,Nsset,我正在将字典(单词列表,而不是类)作为NSStrings加载到NSSet中。然后我反复向这个集合发送消息-containsObject:someNSString。但它总是返回false。我编写了一些代码来测试它: NSLog(@"Random from dictionary: %@", [dictionary anyObject]); NSString *test = [NSString stringWithFormat:@"BEMIRED"]; NSLog(@"To match this wo

我正在将字典(单词列表,而不是类)作为NSStrings加载到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"}
0:{contents=“BEMIRED”}
3:{contents=“SUBJECTIFIED”}
有人能帮我吗? 谢谢

似乎只检查指针值(该方法的文档非常缺乏),而不检查对象是否相等。因此,您需要改用,它使用
isEqual:
检查被认为相等的对象是否在您的集合中

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


Edit:实际上,似乎
containsObject:
也使用了
isEqual:
。它们似乎只在返回的内容上有所不同(
containsObject:
返回
BOOL
,而
成员:
返回
id
)。出于文档的目的,我保留这个答案。

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


所以我猜你们的弦实际上是不相等的。我会检查隐藏的空格或其他类似的问题。

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

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


对于未来的论坛访客,我想提请大家注意DarkDust和zoul关于-containsObject和-member(NSSet的两种方法)的讨论,结果都使用了-isEqual方法。

BEMIRED应该是粗体的。它实际上没有在日志中显示**之前和之后。对不起,我第一次在这里发帖。你能告诉我们
字典是如何创建的吗?也许是个愚蠢的建议,但你试过
[[dictionary allValues]containsObject:…]
?我用的是Dave DeLongs DDFileReader:这不是一个不相关的问题,你能在回答中解释这个问题吗?这对我们来说可能很有意思。这太疯狂了,你有什么建议来解释这种行为吗?你测试过了吗?根据
containsObject:
应该像预期的那样使用对象相等。它似乎在那里也不起作用。我开始觉得我读文件的方式有问题。@zoul:你说得对。考虑到
containsObject:
的文档中没有提到任何具体内容,但是
成员:
明确表示它正在使用
isEqual:
,我假设
containsObject:
只会检查指针值,这会非常快。我搜索了一个确认,但除了你引用的帖子(上面说我错了),我什么也找不到。