这是几行代码... currentPage 是一个 NSInteger,imageViewHolder 是一个 NSMutableArray。
NSLog(@"currentPage: %d, imageViewHolderCount: %d", currentPage, [imageViewHolder count]);
if(currentPage < [imageViewHolder count] - 1)
{
NSLog(@"%d < %d - 1", currentPage, [imageViewHolder count]);
tempView = [imageViewHolder objectAtIndex:currentPage + 1];
NSLog(@"doesn't get here, crashed."); //this doesn't get logged because of crash.
//do other stuff here
}
由于索引超出范围,我在“tempView = ...”行崩溃。这是我得到的输出:
currentPage: 0, imageViewHolderCount: 0
0 < 0 - 1
*** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 1 beyond bounds for empty array'
*** First throw call stack:
(0x339bd88f 0x324af259 0x339069db 0x48f1b 0x48c1b 0x35603a85 0x35603409 0x35602c57 0x35716b0f 0x3560424d 0x4abfb 0x324ab175 0x43f77 0x45b91 0x479a7 0x36bff 0x285ed 0x35604ecb 0x32935933 0x33991a33 0x33991699 0x3399026f 0x339134a5 0x3391336d 0x32e91439 0x35608e7d 0xfa01 0xf9c0)
我确定我遗漏了一些明显的东西......为什么我的 if 语句被评估为真的?我相当肯定 0 实际上大于 -1 :)