xcode [__NSArrayM objectAtIndex:]: 索引 0 超出空数组的边界
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13663093/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
[__NSArrayM objectAtIndex:]: index 0 beyond bounds for empty array
提问by user1695971
I am getting this error on my project in Xcode;
我在 Xcode 中的项目中收到此错误;
NSRangeException [__NSArrayM objectAtIndex:]: index 0 beyond bounds for empty array
NSRangeException [__NSArrayM objectAtIndex:]: 索引 0 超出空数组的边界
I think the line is here in my initial controller.m file;
Session *item = [dataArray objectAtIndex:0];
NSString* strDate = [LoadSessionController GetTimeString:item.mCreateDate];
cell1.title = @"Restore last session";
cell1.date = strDate;
cell1.ago = [LoadSessionController GetBeforeTimeString:item.mCreateDate];
cell1.image = item.mThumbnail;
cell1.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
cell1.selectionStyle = UITableViewCellSelectionStyleBlue;
cell = cell1;
break;
}
As the crash log is this;
Exception Type: EXC_CRASH (SIGABRT)
Exception Codes: 0x0000000000000000, 0x0000000000000000
Crashed Thread: 0
Last Exception Backtrace:
0 CoreFoundation 0x328c429e __exceptionPreprocess + 158
1 libobjc.A.dylib 0x395a697a objc_exception_throw + 26
2 CoreFoundation 0x3280fb70 -[__NSArrayM objectAtIndex:] + 160
3 ColorSplash 0x0001722a 0x1000 + 90666
4 UIKit 0x3861f540 -[UITableView(UITableViewInternal) _createPreparedCellForGlobalRow:withIndexPath:] + 408
5 UIKit 0x38604306 -[UITableView(_UITableViewPrivate) _updateVisibleCellsNow:] + 1306
6 UIKit 0x3861b7c2 -[UITableView layoutSubviews] + 202
7 UIKit 0x385d77fe -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 254
8 QuartzCore 0x3a2abd5e -[CALayer layoutSublayers] + 210
9 QuartzCore 0x3a2ab8fc CA::Layer::layout_if_needed(CA::Transaction*) + 456
10 QuartzCore 0x3a2ac830 CA::Layer::layout_and_display_if_needed(CA::Transaction*) + 12
11 QuartzCore 0x3a2ac216 CA::Context::commit_transaction(CA::Transaction*) + 234
12 QuartzCore 0x3a2ac024 CA::Transaction::commit() + 312
13 UIKit 0x385dd8e6 _afterCACommitHandler + 122
14 CoreFoundation 0x328996c8 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 16
15 CoreFoundation 0x328979bc __CFRunLoopDoObservers + 272
16 CoreFoundation 0x32897d12 __CFRunLoopRun + 738
17 CoreFoundation 0x3280aeb8 CFRunLoopRunSpecific + 352
18 CoreFoundation 0x3280ad44 CFRunLoopRunInMode + 100
19 GraphicsServices 0x35d4f2e6 GSEventRunModal + 70
20 UIKit 0x386282f4 UIApplicationMain + 1116
But I am convinced the array isn't empty, but perhaps the newer version of iOS requires a different answer to just the :0
但我确信数组不是空的,但也许较新版本的 iOS 需要一个不同的答案:0
Any help would be much appreciated,
任何帮助将非常感激,
Thanks,
谢谢,
Chris
克里斯
EDIT - Some more info;
编辑 - 更多信息;
- (void)viewDidLoad
{
CGRect frame = CGRectMake(0, 0, SCN_WIDTH, SCN_HEIGHT);
[self.view setFrame: frame];
self.dataArray = [LoadSessionController loadTableData:NO];
self.tableView.rowHeight = ROW_HEIGHT;
}
...Loadsessioncontroller is;
...Loadsessioncontroller 是;
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *kCustomCellID = @"MyCellID";
CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:kCustomCellID];
if (cell == nil)
{
cell = (CustomCell *)[[[CustomCell alloc] initWithFrame:CGRectZero reuseIdentifier:kCustomCellID] autorelease];
}
Session *item = [dataArray objectAtIndex:[self getDataIndex:indexPath.row]];
NSString* strDate = [LoadSessionController GetTimeString:item.mCreateDate];
cell.title = @"Load saved session";
cell.date = strDate;
cell.ago = [LoadSessionController GetBeforeTimeString:item.mCreateDate];
cell.image = item.mThumbnail;
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
return cell;
}
NSInteger TimeSort(Session *obj1, Session *obj2, void *reverse) {
if ((NSInteger *)reverse == NO) {
if ([obj1.mCreateDate timeIntervalSinceNow] < [obj2.mCreateDate timeIntervalSinceNow])
return NSOrderedAscending;
else
return NSOrderedDescending;
}
else {
if ([obj1.mCreateDate timeIntervalSinceNow] > [obj2.mCreateDate timeIntervalSinceNow])
return NSOrderedAscending;
else
return NSOrderedDescending;
}
}
+ (NSMutableArray*) loadTableData:(BOOL) bLoadRestoreData
{
NSMutableArray* ret = [[NSMutableArray alloc] init];
NSString* path =[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
int i;
int nStartNo = 1;
if (bLoadRestoreData)
nStartNo = 0;
for (i = nStartNo; i < 100; i++)
{
if ([Session exist:path index:i])
{
Session* one = [[Session alloc] init];
[one read:path index:i];
[ret addObject:one];
[one release];
}
}
[ret sortUsingFunction:TimeSort context:self];
return ret;
}
+ (NSString*) GetTimeString:(NSDate *)date
{
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *components = [calendar components:
(kCFCalendarUnitHour|kCFCalendarUnitMinute|kCFCalendarUnitYear|kCFCalendarUnitMonth|kCFCalendarUnitDay) fromDate:date];
//[dateFormatter stringFromDate:date]
return [NSString stringWithFormat:@"Saved %02d/%02d/%02d %02d:%02d", components.year, components.month, components.day,
components.hour, components.minute];
}
#define TIME_SUFFIX(a) (((a)>1)?"s":"")
+ (NSString*) GetBeforeTimeString:(NSDate*) date
{
NSTimeInterval ago = [date timeIntervalSinceNow];
unsigned int temp = -ago;
int days = temp / (3600*24); temp = temp % (3600 * 24);
int hours = temp / 3600; temp = temp % 3600;
int minutes = temp / 60; temp = temp % 60;
int seconds = temp;
if (days == 0 && hours == 0 && minutes == 0)
{
return [NSString stringWithFormat:@"%d second%s ago",
seconds, TIME_SUFFIX(seconds)];
}
else if (days == 0 && hours == 0)
{
return [NSString stringWithFormat:@"%d minute%s %d second%s ago",
minutes, TIME_SUFFIX(minutes), seconds, TIME_SUFFIX(seconds)];
}
else if (days == 0)
{
return [NSString stringWithFormat:@"%d hour%s %d minute%s ago",
hours, TIME_SUFFIX(hours), minutes, TIME_SUFFIX(minutes)];
}
else {
return [NSString stringWithFormat:@"%d day%s %d hour%s ago",
days, TIME_SUFFIX(days), hours, TIME_SUFFIX(hours)];
}
}
回答by rdelmar
"But I am convinced the array isn't empty, but perhaps the newer version of iOS requires a different answer to just the :0"
“但我确信数组不是空的,但也许较新版本的 iOS 需要一个不同的答案:0”
No, this is not the case. Your array is definitely empty. Just log your array before the statement where you think the problem is, and see what you get. If you need to check whether an array is empty, you can either check if array.count is 0 or if array.lastObject is nil.
不,情况并非如此。你的数组肯定是空的。只需在您认为问题所在的语句之前记录您的数组,然后看看您会得到什么。如果您需要检查数组是否为空,您可以检查 array.count 是否为 0 或 array.lastObject 是否为 nil。
回答by D_4_ni
As your error message says, your dataArray
is empty. Either you don't fill it at all, or you access it before filling it.
正如您的错误消息所说,您dataArray
是空的。要么根本不填充它,要么在填充之前访问它。
回答by Ramy Al Zuhouri
The array is empty for sure, verify it through an assertion:
数组肯定是空的,通过断言验证它:
NSAssert( [dataArray count] >=1, @"Array is empty");
Then post the code with which you think to fill the array.
然后发布您认为用于填充数组的代码。