【发布时间】: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