0

I have two custom objects: Movie and MovieTheater. In MovieTheater I have an NSMutableArray that I'm required to use to add movies. This is my code:

// theater 1
    MovieTheater* tempTh1 = [[MovieTheater alloc]init];
    Movie* tempMv1 = [[Movie alloc]init];

    if (tempTh1 != nil) {

        // set up a theather
        tempTh1.theaterName = @"Regal";
        tempTh1.theaterLocation = @"35335 Riverside,CA 92506";
        tempTh1.theaterImage = [UIImage imageNamed:@"theatremain.ashx.jpg"];

        // set up movie 1 ------------------------------------------------------------------


        if (tempMv1 != nil) {

            tempMv1.movieName = @"The Zero Theorem";
            tempMv1.movieTime = @"SAT 18:30";
            tempMv1.movieImage = [UIImage imageNamed:@"zerotheorem.jpg"];
            tempMv1.movieTrailer = [NSURL URLWithString:@"http://youtu.be/XpsJPOMfGSo"];

            // add movie to theater array
            [tempTh1.theaterMovies insertObject:tempMv1 atIndex:0];
        }



        // set up movie 2 ------------------------------------------------------------------
        Movie* tempMv2 = [[Movie alloc]init];

        if (tempMv2 != nil) {

            tempMv2.movieName = @"Welcome to the Jungle";
            tempMv2.movieTime = @"TH 16:30";
            tempMv2.movieImage = [UIImage imageNamed:@"welcometothejungle.jpg"];
            tempMv2.movieTrailer = [NSURL URLWithString:@"http://youtu.be/kRzSImxqQtA"];
        }

        // add movie to theater array
        [tempTh1.theaterMovies insertObject:tempMv2 atIndex:1];

        // set up movie 3 ------------------------------------------------------------------
        Movie* tempMv3 = [[Movie alloc]init];

        if (tempMv3 != nil) {

            tempMv3.movieName = @"The Bag Man";
            tempMv3.movieTime = @"TUE 15:30";
            tempMv3.movieImage = [UIImage imageNamed:@"bagman.jpg"];
            tempMv3.movieTrailer = [NSURL URLWithString:@"http://youtu.be/mKBLfwVEKP8"];
        }

        // add movie to theater array
        [tempTh1.theaterMovies insertObject:tempMv3 atIndex:2];

        // set up movie 4 ------------------------------------------------------------------
        Movie* tempMv4 = [[Movie alloc]init];

        if (tempMv4 != nil) {

            tempMv4.movieName = @"Better Living Through Chemistry";
            tempMv4.movieTime = @"MO 16:45";
            tempMv4.movieImage = [UIImage imageNamed:@"betterlivingthroughchemistry.jpg"];
            tempMv4.movieTrailer = [NSURL URLWithString:@"http://youtu.be/qxHPqHIl11w"];
        }

        // add movie to theater array
        [tempTh1.theaterMovies insertObject:tempMv4 atIndex:3];

        // set up movie 5 ------------------------------------------------------------------
        Movie* tempMv5 = [[Movie alloc]init];

        if (tempMv5 != nil) {

            tempMv5.movieName = @"Blood Ties";
            tempMv5.movieTime = @"FR 19:00";
            tempMv5.movieImage = [UIImage imageNamed:@"bloodties.jpg"];
            tempMv5.movieTrailer = [NSURL URLWithString:@"http://youtu.be/DI86othXjho"];
        }

        // add movie to theater array
        [tempTh1.theaterMovies insertObject:tempMv5 atIndex:4];


        // add the theater to the global array
        [cinemas insertObject:tempTh1 atIndex:0];

        NSLog(@"%@", tempTh1.theaterMovies);
    }

The problem I have is that [tempTh1.theaterMovies insertObject:tempMv1 atIndex:0]; does not add anything. The NSLog shows the array theaterMovies nil. What am I doing wrong?

5
  • Why don't you use -[NSMutableArray addObject:] ? Commented Jan 28, 2014 at 17:34
  • 1
    MovieTheater* tempTh1 = [[MovieTheater alloc]init]; - you should initialise theaterMovies there (in the -(id)init implementation) with an empty NSMutableArray or at any point later but before you are starting to add objects to it. Commented Jan 28, 2014 at 17:36
  • @singingAtom, I could use that, but insertObject: should work as well, right ? Commented Jan 28, 2014 at 17:36
  • @Nactus have you initialized the theterMovies array? Commented Jan 28, 2014 at 17:38
  • I don't see any sign of creating either of the arrays. Commented Jan 28, 2014 at 18:07

3 Answers 3

1

Initialize tempTh1.theaterMovies in the init method of MovieTheater or just before you start adding objects with an empty NSMutableArray like

tempTh1.theaterMovies = [NSMutableArray array];
Sign up to request clarification or add additional context in comments.

2 Comments

sorry for the duplicate - is formulated my answer in the comments and then as an actual answer - its the same solution the others suggested :)
I actually glad you use the "answer" option instead of the comment, this way I can accept your answer.
1

In the init method of the MovieTheater class you need to allocate the array:

_theaterMovies = [[NSMutableArray alloc] init];

Comments

1

The theaterMovies array is nil. You are probably never actually setting the property to point to an array. You'll probably want to do that in MovieTheater's init method.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.