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

标签 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

知道为什么会出错吗?

最佳答案

这可以帮助您了解:

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);

关于ios - nsmutablearray.count和tableview indexpath.row之间的比较给出了奇怪的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35197943/

相关文章:

ios - NSUserDefaults 似乎没有创建全局可访问的变量

ios - GPUImage 着色器因 "ERROR: One or more attached shaders not successfully compiled"而崩溃

类型 Dictionary<string,string> 不支持 C# 比较运算符

c - LibXML2 中有 "binary dump"或 "get binary representation"函数吗?

ruby-on-rails - 如何让变量保留 2 位小数

ios - 将图像编码为 base64String 的正确方法

ios - 如何在 Objective-c 中使用元编程

ios - 在 Xcode 中与您的设备通信时出错

ios - 移除iPhone键盘上方的“横条”

ios - 使用 AFNetworking 进行 SSL 证书验证