我在Xcode中的项目中收到此错误;
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
但我确信数组不是空的,但也许新版本的iOS需要一个不同的答案:0
非常感谢任何帮助,
谢谢,
克里斯
编辑 - 更多信息;
- (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是;
- (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)];
}
}
答案 0 :(得分:3)
“但我确信阵列不是空的,但也许新版本的iOS只需要一个不同的答案:0”
不,事实并非如此。你的数组肯定是空的。只需在您认为问题所在的语句之前记录您的数组,并查看您获得的内容。如果需要检查数组是否为空,可以检查array.count是否为0或者array.lastObject是否为nil。
答案 1 :(得分:1)
正如您的错误消息所示,您的dataArray
为空。要么根本没有填写,要么在填写之前访问它。
答案 2 :(得分:-1)
数组是空的,请通过断言验证:
NSAssert( [dataArray count] >=1, @"Array is empty");
然后发布您认为要填充数组的代码。