【问题标题】:Index 0 beyond bounds [0 .. 0], but not an empty array索引 0 超出范围 [0 .. 0],但不是空数组
【发布时间】:2012-07-16 07:55:05
【问题描述】:

我有一个UITableView 用于我正在创建的一个将显示电影预告片的应用程序。

在我的RootViewController.h 文件中,我有这段代码(这里提供了所需的所有相关代码):

@interface RootViewController : UITableViewController {
    NSMutableArray *listOfLinks;
}

在我的 .m 中有这段代码:

- (void)viewDidLoad {
    [super viewDidLoad];
    listOfLinks = [[NSMutableArray alloc] init];
    NSURL *myurl2 = [NSURL URLWithString:@"http://website.com/movietrailers.plist"];
    NSDictionary *plistDict2 = [NSDictionary dictionaryWithContentsOfURL:myurl2];
    NSArray *plistArray2 = [plistDict2 objectForKey:@"MovieLinks"];
    NSDictionary *dic = [NSDictionary dictionaryWithObject:plistArray2 forKey:@"MovieLinks"];
    [listOfLinks addObject:dic];
    NSLog(@"%@", listOfLinks);
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSURL *movieURL = [listOfLinks objectAtIndex:indexPath.row];
    MPMoviePlayerViewController *moviePlayer = [[MPMoviePlayerViewController alloc] initWithContentURL:movieURL];
    [self presentMoviePlayerViewControllerAnimated:moviePlayer];
    [moviePlayer release];
}

我的 plist 如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>MovieTitles</key>
    <array>
        <string>Movie 1</string>
        <string>Movie 2</string>
    </array>
    <key>MovieLinks</key>
    <array>
        <string>http://trailerserver.com/movie1.mp4</string>
        <string>http://trailerserver.com/movie2.mp4</string>
    </array>
</dict>
</plist>

但我基本上得到了listOfLinks 为空的错误。错误如下所示:

Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 6 beyond bounds [0 .. 0]'

当我NSLog 数组时,它会打印出它的字符串。这是什么原因?感谢您的帮助!

【问题讨论】:

  • 这并不意味着您的数组是空的。这意味着您的数组有 5 个值并且您正在尝试访问 6 个值...尝试执行某种检查您在哪里得到错误。
  • @iPhoneDeveloper out of bounds [0..0] 几乎意味着它是空的。没有?
  • 不,这意味着它有一个项目,你添加到它的 NSDictionary
  • @Encephalon 在 didSelectRow... 中访问它之前,您是否尝试过 NSLog?也许在此之前(以及在 ViewDidLoad 完成之后)改变了数组的内容
  • 伍迪的回答可能是对的。只是为了确保您可以提供 NSLog(@"%@", listOfLinks); 的完整输出。陈述。它应该告诉您确切的数据结构。一般来说,建议测试您收到的对象类型以及 objectForKey: 等结果的“nil”。当您与某个服务器通信时,您永远无法确定,即使它是您自己的服务器.

标签: iphone xcode nsarray nsurl nslog


【解决方案1】:

您向数组中添加了 1 个字典,现在正尝试获取第 6 项,但您只有一个。当您记录它时,您会看到那里的单个字典的内容。

当你执行 numberOfRowsInSection 时,你可能返回的是字典的计数,而不是数组,我猜你想要字典,所以你应该有

NSDictionary* d = [listOfLinks objectAtIndex:0];
NSURL *movieURL = [[listOfLinks [d allKeys]] objectAtIndex: indexPath.row];
MPMoviePlayerViewController *moviePlayer = [[MPMoviePlayerViewController alloc] initWithContentURL:movieURL]

或者类似的东西

编辑为您更新的问题。

我不确定您为什么将信息存储在字典中。实际上,您从字典中获取元素数组,然后用它创建字典并将其放入数组中。这没有任何意义。

在你所在的代码部分:

NSDictionary *plistDict2 = [NSDictionary dictionaryWithContentsOfURL:myurl2];
NSArray *plistArray2 = [plistDict2 objectForKey:@"MovieLinks"];

此时您似乎有一个数组,那么您为什么要这样做:

NSDictionary *dic = [NSDictionary dictionaryWithObject:plistArray2 forKey:@"MovieLinks"];
[listOfLinks addObject:dic];

创建字典并将字典放入数组的元素 0 中?

如果您希望 listOfLinks 包含从 MovieLinks 键获得的链接列表,为什么不将它们直接放入数组中?

if(plistArray2 != nil && [plistArray2 count] > 0)
    [listOfLinks addObjectsFromArray:plistArray2];

然后在你的访问器中:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if(section == WHATEVERSECTION)
        return [listOfLinks count];
    return 0;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{
    NSURL *movieURL = [listOfLinks objectAtIndex:indexPath.row];
    MPMoviePlayerViewController *moviePlayer = [[MPMoviePlayerViewController alloc] initWithContentURL:movieURL];
    [self presentMoviePlayerViewControllerAnimated:moviePlayer];
    [moviePlayer release];
}

【讨论】:

  • 谁说过第 6 项?主题建议索引为 0
  • oops ;) 我的错,对不起)) 我没有完全阅读错误,假设 OP 在主题中正确引用了它;)
  • 好吧,在这种情况下,它是第 7 项。 :)
  • 是第 6 个索引,即第 6 个元素,索引 5,还是索引 6,如 0,1,2,3,4,5,6,第 7 个?不管怎样,距离0号还有很长的路要走! :)
  • Woody,你完全正确,但我仍然收到错误,这次在编译之前:“数组下标不是整数”。我更新了我的问题以显示我的 plist 的样子。
【解决方案2】:

检查表视图的 numberOfRowsInSection 方法。您在此方法中返回的内容。写

 [listOfLinks count] 
在这个方法中

【讨论】:

    【解决方案3】:

    您将 NSDictionary 放入 listOFLinks 中,然后尝试检索 NSURL,这是错误的。

    您需要获取字典内和数组内的 URL。

    祝你好运!

    【讨论】:

      猜你喜欢
      • 2016-12-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-07
      • 1970-01-01
      • 2019-09-13
      • 1970-01-01
      相关资源
      最近更新 更多