nsmutablearray.count和amp;之间的比较tableview indexpath.row给出了奇怪的结果

时间:2016-02-04 10:04:14

标签: ios objective-c comparison

我在下面。

NSLog(@"aaaa===%ld==%ld", indexPath.row, (rowsOptionss.count-1));

if (indexPath.row>(rowsOptionss.count-1)) {
    NSLog(@"000===this is true");
} else {
    NSLog(@"000===this is false");
}

if (0>-1) {
    NSLog(@"001===this is true");
} else {
    NSLog(@"001===this is false");
}

这给我的结果如下。

2016-02-04 13:02:15.316 ClubsFans[7085:16944474] aaaa===0==-1
2016-02-04 13:02:15.316 ClubsFans[7085:16944474] 000===this is false
2016-02-04 13:02:15.316 ClubsFans[7085:16944474] 001===this is true

我应该说

2016-02-04 13:02:15.316 ClubsFans[7085:16944474] aaaa===0==-1
2016-02-04 13:02:15.316 ClubsFans[7085:16944474] 000===this is true >> this should be true
2016-02-04 13:02:15.316 ClubsFans[7085:16944474] 001===this is true

知道为什么会出错?

2 个答案:

答案 0 :(得分:5)

这可以帮助您理解:

NSIndexPath* indexPath = nil;
NSArray * rowsOptionss = [NSArray array];
NSLog(@"aaaa===%ld==%ld", indexPath.row, (rowsOptionss.count-1));
NSLog(@"real value: bbbb===%ld==%u", (long)indexPath.row, (rowsOptionss.count-1));

if (indexPath.row>(rowsOptionss.count-1)) {
    NSLog(@"000===this is true");
} else {
    NSLog(@"000===this is false");
}

if (0>-1) {
    NSLog(@"001===this is true");
} else {
    NSLog(@"001===this is false");
}

rowsOptionss.count是未签名的(NSUInteger),如果您执行-1操作,您将获得一个非常大的正数,总是大于0。

这是日志:

 aaaa===0==-1
 real value: bbbb===0==4294967295
 000===this is false
 001===this is true

如果您需要签名的int,请改用:

NSInteger tempInt = rowsOptionss.count;
tempInt --;
LOG(@"tempInt %i", tempInt);

答案 1 :(得分:0)

@Fahim Parkar

请尝试将 NSInteger 转换为 Int 。这可能会导致比较NSInteger

  

if((int)indexPath.row>(int)(rowsOptionss.count-1))

希望这会对你有所帮助。