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

將打印0.爲什麼?obj -c NSArray計數比較失敗

回答

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

+1我盯着看那5分鐘的代碼認爲有些東西感覺不對,但我無法把它放在手指上。爲了我的項目,我想我最好今天做一些文書工作。 – 2009-11-10 14:55:38

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)