Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

Problem with matching data from 2 different arrays

Hi,

I have 2 different arrays - cinema "screenings" and "movies". In the "screenings" array are all the screenings with data such as start time, available seats etc for each screening. In the "movies" array are all the movies with data such as movie name, length etc for each movie. I want to display a screening start time and the name of the movie for that screening in each table view cell, but I can't figure out how to get the correct movie name for each screening as the data is in 2 different arrays. I know I have to use the "id"'s of the screening and the movie to match them, but how exactly and with what method? I tried the NSArray method "indexOfObject:" and looked Apple's example code, but couldn't quite figure it out.

It's rather difficult to explain in detail, so here's a dropbox link to the Xcode project: https://www.dropbox.com/sh/2vjp30fy0cxsvlr/AAAk7U-Lb4VcL052tm7df-cka

Thanks in advance! :)

1 Answer

I looked over you code. One way you could accomplish this is:

NSMutableDictionary *dict = [NSMutableDictionary dictionary];

    for (Screening *movieData in self.screenings) {
        for (Screening *screeningData in self.movies) {
            if ([movieData.movieID isEqual:screeningData.movieID]) {
               //fill the dictionary with name and corresponding ID which you can use later 
            }
        }
    }

Unfortunately though, this won't work. The reason being, the 'screening' object's 'screeningMovieID' and 'movies' object's 'movieID' is not the same. (my understanding is that it should be). Since it's not, it's kinda hard to communicate between the two objects.

Having said that, if I were you, I'd take a look at your model and change quite a bit. I know this looks quite similar to the blog reader project, with the way you make a request, parse it, and store it in model object.

Think of it this way. The model should be a 'black box'. You just say “give me all movie details” or some other high-level command and some time later, you get a nice list of the results or an error, which is displayed by the viewController.

Hope this was a little bit helpful at the very least ;)

Thank you for a quick answer, Thomas! It was definitely useful. I've been busy with some other stuff, but I'll post my solution once I get it done.