obj-c NSArray计数比较失败

时间:2009-11-10 14:46:52

标签: objective-c comparison count nsarray

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.为什么?

2 个答案:

答案 0 :(得分: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.

答案 1 :(得分: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)