gpt4 book ai didi

ios - 将数据插入属于自定义对象的 NSMutableArray

转载 作者:行者123 更新时间:2023-11-28 20:07:54 25 4
gpt4 key购买 nike

我有两个自定义对象:Movie 和 MovieTheater。在 MovieTheater 中,我有一个 NSMutableArray,我需要用它来添加电影。这是我的代码:

// 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);
}

我遇到的问题是 [tempTh1.theaterMovies insertObject:tempMv1 atIndex:0];不添加任何东西。 NSLog 显示数组 theaterMovies nil。我究竟做错了什么?

最佳答案

在 MovieTheater 的 init 方法中初始化 tempTh1.theaterMovies,或者在您开始添加具有空 NSMutableArray 的对象之前,例如

tempTh1.theaterMovies = [NSMutableArray array];

关于ios - 将数据插入属于自定义对象的 NSMutableArray,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21412613/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com