這裏有幾行代碼... 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 :)
數組的計數是無符號的。如果你打印'([imageViewHolder count] - 1)'?會發生什麼? –