objective-c - Objective-C-循环播放但循环条件为假

标签 objective-c crash for-loop infinite-loop conditional-operator

我正在尝试使用此函数将分区表转换为平面列表到didSelectRowAtIndexPath(我有一个NSArray初始化为每个节中包含的项目数):

某处... :-)

self.sectionsArray = [NSArray arrayWithObjects:macroIntNumber(1), macroIntNumber(3), macroIntNumber(12), nil];

然后进入didSelectRowAtIndexPath:
 int selectedRow = 0;
 int a = indexPath.section;

 for (int i=0; i<indexPath.section-1; i++) {
  selectedRow += [[self.sectionsArray objectAtIndex:i] intValue];
 }
 selectedRow += indexPath.row;

但是...对于indexPath.section = 0(第一部分),这将崩溃。
因为循环会无限循环播放直到NSArray调用崩溃...
奇怪!

强制for (int i=0; i<0-1; i++) {工作

强制for (int i=0; i<a-1; i++) {工作

我想念什么?

最佳答案

sectionNSUInteger,因此未签名。因此,从该无符号整数的0中减去1将使您获得一个非常大的数字,而不是-1。

使用a时可以使用,因为您已将a声明为int。 :)

关于objective-c - Objective-C-循环播放但循环条件为假,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4700608/

相关文章:

linux-kernel - 在panic()发生之前使用trace-cmd/ftrace获取function_graph

Android WallpaperManager.setResource 崩溃 Activity

java - 我尝试运行这段代码,它运行时不会抛出错误,问题是它不会执行 for 循环我只是一个初学者,所以请

javascript - 使用 jQuery 在增量 ID 输入之间进行迭代

ios - 进入后台暂停游戏

objective-c - 如何检验一个数的平方根是否有理数?

objective-c - 如何在 iOS 8 Today Extension 上显示键盘?

crash - XCode 7.2 在协议(protocol)扩展实现功能不正确时崩溃

delphi - TStringList .add 从随机函数中生成重复项

iphone - NS 字符串比较因 stringWithFormat 失败