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を出力します。なぜですか?

4

2 に答える 2

6

最初のテスト [test1 count] == [test2 count] は true (または 1) を返しますが、2 番目のテスト 1 == [test3 count] は 2 つの要素があるため失敗すると推測します。おそらく代わりに ([test1 count] == [test2 count]) && ([test2 count] == [test3 count]) と言いたいでしょう。これは、推移的なプロパティを使用して等しいかどうかをテストします。つまり、A == B および B == C の場合、A == C です。

于 2009-11-10T14:51:31.677 に答える
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)
于 2009-11-10T14:56:30.273 に答える