0

I have a UITableView for an app I am creating that will show Movie Trailers.

In my RootViewController.h file I have this code (well all of the relevant code needed is supplied here):

@interface RootViewController : UITableViewController {
    NSMutableArray *listOfLinks;
}

In my .m is this code:

- (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];
}

My plist looks like this:

<?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>

But I am essentially getting the error that listOfLinks is empty. The error looks like this:

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

When I NSLog the array it prints out it's strings. What is the reason for this? Thanks for your help!

Kevin Bedell
  • 13,254
  • 10
  • 78
  • 114
Encephalon
  • 153
  • 2
  • 12
  • It does not mean that your array is empty.It means that your array has 5 values and you are trying to access the 6 value...try to perform some kind of check where you are getting the error. – Tripti Kumar Jul 16 '12 at 07:59
  • @iPhoneDeveloper `out of bounds [0..0]` pretty much means it is empty. no? – poncha Jul 16 '12 at 07:59
  • 1
    No, it means it has one item, the NSDictionary that you added to it – Woody Jul 16 '12 at 08:00
  • @Encephalon did you try `NSLog` right before accessing it in didSelectRow... ? maybe something alters the contents of the array before that (and after ViewDidLoad finished) – poncha Jul 16 '12 at 08:00
  • Woody's answer is probably right. Just to be sure you could provide the complete output of the NSLog(@"%@", listOfLinks); statement. It should tell you the exact data structure. In general it is advisable to test for the object type that your received as well as for 'nil' as result of objectForKey: etc. As you are communicating with some server, you never know for sure, even if it is your very own server. – Hermann Klecker Jul 16 '12 at 08:16

3 Answers3

2

You added 1 dictionary to an array and are now trying to get the 6th item, you only have one. When you log it, you see the contents of the single dictionary you have there.

When you are doing the numberOfRowsInSection you are probably returning the count of the dictionary, not the array, and I am guessing you want the dictionary, so you should have

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

or something along those lines

EDIT for your updated question.

I am not sure why you are storing the info in a dictionary. In fact you are getting an array of elements from a dictionary, then creating a dictionary with it and putting it in an array. That makes no sense.

In your part of the code where you go:

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

this would appear you have an array at that moment, so why do you do this:

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

which creates a dictionary and puts the dictionary in element 0 of an array?

If you want listOfLinks to contain a list of links that you get from the MovieLinks key, why not put them straight into the array?

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

then in your accessors:

- (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];
}
Woody
  • 5,052
  • 2
  • 22
  • 28
  • Who said anything about 6th item? subject suggests index was 0 – poncha Jul 16 '12 at 08:02
  • oops ;) my bad, sorry)) i didnt read the error fully, assumed OP quoted it in subject correctly ;) – poncha Jul 16 '12 at 08:06
  • Well, in that case it is the 7th item though. :) – Hermann Klecker Jul 16 '12 at 08:12
  • Is it the 6th index ie, the 6th element, index 5, or index 6 as in 0,1,2,3,4,5,6, the 7th? Either way, it is long way from the 0th! :) – Woody Jul 16 '12 at 08:28
  • Woody, you are totally right, but I am still getting an error, this time before compiling: "Array subscript is not an integer". I've updated my question to show what my plist looks like. – Encephalon Jul 16 '12 at 17:48
  • Thanks for all of your help Woody! I've done everything you've suggested, and feel like I'm so close! I'm getting this error now though: Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFString absoluteURL]: unrecognized selector sent to instance – Encephalon Jul 16 '12 at 19:24
  • Try NSUrl *movieURL = [NSURL URLWithString:[listOfLinks objectAtIndex:indexPath.row]]; – Woody Jul 16 '12 at 20:26
  • You sir are a freaking genius! Definitely owe you so much! – Encephalon Jul 16 '12 at 20:39
0

Check table view's numberOfRowsInSection method. What you are returning in this method. write

 [listOfLinks count] 
in this method
spaleja
  • 1,435
  • 15
  • 24
0

You are putting a NSDictionary inside the listOFLinks and then you are trying to retrieve a NSURL, that's wrong.

You need to get the URL that is inside the dictionary and inside the array.

Good luck!

Antonio MG
  • 20,382
  • 3
  • 43
  • 62