Rik*_*rdA 13 objective-c nsstring nsset
我正在将一个字典(单词列表,而不是类)加载到NSSet中作为NSStrings.然后我重复发送这个消息-containsObject:someNSString.但它总是返回错误.我写了一些代码来测试它:
NSLog(@"Random from dictionary: %@", [dictionary anyObject]);
NSString *test = [NSString stringWithFormat:@"BEMIRED"];
NSLog(@"To match this word: %@", test);
if ([dictionary containsObject:test])
NSLog(@"YES!");
Run Code Online (Sandbox Code Playgroud)
在日志中我得到以下内容:
Random from dictionary: BEMIRED
To match this word: BEMIRED
Run Code Online (Sandbox Code Playgroud)
(我错过了"是的!")
当我尝试使用CFShow(字典)时,我可以看到它实际上包含字符串和所有内容.一个例子:
0 : <CFString 0xc3bd810 [0x1386400]>{contents = "BEMIRED"}
3 : <CFString 0xdf96ef0 [0x1386400]>{contents = "SUBJECTIFIED"}
Run Code Online (Sandbox Code Playgroud)
有人可以帮我吗?谢谢!
Mik*_*ler 27
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);
}
Run Code Online (Sandbox Code Playgroud)
我们可以看到两个字符串具有不同的指针值,但该集仍然为该containsObject:
调用返回YES .
所以我猜你的字符串实际上并不相同.我会检查隐藏的空白或其他类似的问题.
归档时间: |
|
查看次数: |
7955 次 |
最近记录: |