obj-c NSArray计数比较失败

qua*_*ano 1 comparison objective-c 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]);
Run Code Online (Sandbox Code Playgroud)

会打印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.