本文介绍了iOS错误:[__ NSArrayI objectAtIndex:]:索引1超出范围[0 .. 0]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,我尝试从url加载内容,将它们存储在可变数组中,并在表视图中显示它们.但是我无法正常运行,因为每次我运行该应用程序时都会出现此错误:

In my application I try to load contents from a url, store them in a mutable array and show them in a table view. But I can't get it working, because every time I run the app this error appears:

*** Terminating app due to uncaught exception 'NSRangeException',
reason: '*** -[__NSArrayI objectAtIndex:]: index 1 beyond bounds [0 .. 0]'
*** First throw call stack:
(0x34dd088f 0x36d9e259 0x34d2823d 0x316e562f 0x315f09a9 0x313c0c5d 0x313c1b95 0x313c1ae7
 0x313c16c3 0xa5cfb 0x33623ec7 0x35387a09 0x35390051 0x33622965 0xa4dc1 0x313a8e33
 0x313cd629 0x31391d7d 0x314544dd 0x3139a55d 0x3139a579 0x3139a40b 0x3139a3e7 0x313a8015
 0x313a1985 0x3136fc6b 0x3136f70f 0x3136f0e3 0x3439222b 0x34da4523 0x34da44c5 0x34da3313
 0x34d264a5 0x34d2636d 0x313a0a13 0x3139de7d 0xa4745 0xa46dc)
terminate called throwing an exception

我创建了一个数组,该数组应使用以下命令填充我的viewDidLoad中的表:

I create the array that should populate the table in my viewDidLoad with:

_videos = [[NSMutableArray alloc] init];

然后,我连接到URL并通过接收的xml数据进行解析.这项工作应有尽有.打开某个标签后,我创建了我的视频对象,并用数据填充了它们之后,使用以下命令将这些对象添加到我的数组中:

Then I connect to the url and parse through the received xml data. This works just like it should. When a certain tag is opened I create my video objects and after filling them with data I add those objects to my array with:

[_videos addObject:currentVideo];

这似乎也很好,因为它会在以下情况下返回正确的视频数量

This seems to work as well, because it returns the correct number of videos when

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return _videos.count;
}

被调用.但是在此之后,应用程序崩溃了,我什至没有达到尝试填充表格视图的程度.该函数如下所示:

is called. But after this point the app crashes and I don't even reach the point where I try to populate my table view. The function looks like this:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    Video *curVideo = [_videos objectAtIndex:indexPath.row];

    static NSString *CellIdentifier = @"CustomCell";

    CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil)
        cell = [[CustomCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];

    cell.titleLabel.text = [curVideo title];
    cell.descLabel.text = [curVideo desc];

    return cell;
}

怎么了?

预先感谢

推荐答案

在某些地方您可能需要在初始化_videos之前对其进行访问.您最有可能在init之后但在加载视图之前执行此操作.解决此问题的方法是仅使用访问器,并延迟初始化self.videos.这是除initdealloc之外从未直接访问ivars的众多原因之一.

Somewhere you are likely accessing _videos prior to initializing it. Most likely you'd doing it after init, but prior to loading the view. The fix for this kind of problem is to use accessors exclusively, and to lazy-initialize self.videos. This is one of many reasons never to access your ivars directly except in init and dealloc.

@interface ...
@property (nonatomic, readonly, strong) NSMutableArray *videos;
@end

@implementation ...
{
  NSMutableArray *_videos; // Can't auto-synthesize. We override the only accessor.
}

- (NSMutableArray *)videos {
   if (! _videos) {
     _videos = [NSMutableArray new];
   }
   return _videos;
}

现在所有对self.videos的引用都将被初始化,无论它们何时发生.

Now all references to self.videos will be initialized no matter when they happen.

您还可以在init中正确初始化视频,这需要较少的代码:

You can also initialize videos correctly in init, which takes a little less code:

@interface ...
@property (nonatomic, readonly, strong) NSMutableArray *videos;
@end

@implementation ...

- (id)init {
   self = [super init];
   if (self) {
     _videos = [NSMutableArray new];
   }
   return self;
}

这篇关于iOS错误:[__ NSArrayI objectAtIndex:]:索引1超出范围[0 .. 0]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 13:33