开发者

obj-c NSArray count comparison fail

开发者 https://www.devze.com 2022-12-10 20:40 出处:网络
NSArray *test1 = [NSArray arrayWithObjects:@\"1\",@\"2\", nil]; NSArray *test2 = [NSArray array开发者_JAVA技巧WithObjects:@\"1\",@\"2\", nil];
NSArray *test1 = [NSArray arrayWithObjects:@"1",@"2", nil];
NSArray *test2 = [NSArray array开发者_JAVA技巧WithObjects:@"1",@"2", nil];
NSArray *test3 = [NSArray arrayWithObjects:@"1",@"2", nil];

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

Will print 0. Why?


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.


[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)
0

精彩评论

暂无评论...
验证码 换一张
取 消