1
NSArray *test1 = [NSArray arrayWithObjects:@"1",@"2", nil];
NSArray *test2 = [NSArray arrayWithObjects:@"1",@"2", nil];
NSArray *test3 = [NSArray arrayWithObjects:@"1",@"2", nil];

NSLog(@"%d", [test1 count] == [test2 count] == [test3 count]);

将打印 0。为什么?

4

2 回答 2

6

我推测您的第一个测试 [test1 count] == [test2 count] 返回 true(或 1),但是第二个测试 1 == [test3 count] 失败,因为它有 2 个元素。您可能想说 ([test1 count] == [test2 count]) && ([test2 count] == [test3 count]) 代替。使用传递属性测试相等性 - 即如果 A == B 和 B == C 则 A == C。

于 2009-11-10T14:51:31.677 回答
2

[test1 count] == [test2 count] == [test3 count]

将评估为:

[test1 count] == [test2 count] == [test3 count]
= (int of 2) == (int of 2) == [test3 count]
= (BOOL of YES) == (int of 2) // Comparing an implicit 1 with 2 so !=
= (BOOL of NO)
= (int of zero implicit cast)
于 2009-11-10T14:56:30.273 回答