views:

401

answers:

2
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]);

Will print 0. Why?

+6  A: 

I would speculate that your first test [test1 count] == [test2 count] returns true (or 1) but then the second test 1 == [test3 count] fails because it has 2 elements. You probably want to say ([test1 count] == [test2 count]) && ([test2 count] == [test3 count]) instead. That tests for equality using the transitive property - i.e. if A == B and B == C then A == C.

tchester
+1 I stared at that code for 5 minutes thinking something felt wrong, but I couldn't put my finger on it. For the sake of my project, I think I had better do some paperwork today.
e.James
+2  A: 

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

Will evaluate as:

[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)
Ben S