2009-11-10 3 views
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]); 

왜 인쇄됩니까?obj-c NSArray 개수 비교 실패

답변

6

첫 번째 테스트 [test1 count] == ​​[test2 count]가 true (또는 1)를 반환하지만 두 번째 테스트 1 == [test3 count]는 두 개의 요소가 있기 때문에 실패합니다. 대신 ([test1 count] == ​​[test2 count]) & & ([test2 count] == ​​[test3 count])라고 말하고 싶을 것입니다. 이적 속성을 사용하여 평등에 대한 테스트가 - 즉 == B와 B == C 다음 == C.

+1

+1 내가 응시 무언가 잘못되었다고 생각하면서 5 분 동안 코드를 작성했지만 손가락을 집어 넣을 수는 없었습니다. 내 프로젝트를 위해서, 나는 오늘 서류 작업을 더 잘해야한다고 생각한다. –

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)