我正在尝试使用以下代码将我的UIImage
从Web服务添加到我的NSMutableArray:
for (int i=0; i<[totalRowCountString intValue]; i++)
{
NSString *criticsRatingString = [[JSON valueForKeyPath:@"movies.ratings.critics_rating"] componentsJoinedByString:@" "];
NSLog(@"criticsRatingString: %@", criticsRatingString);
if ([criticsRatingString isEqualToString:@"Certified Fresh"])
{
self.ratingImage = [UIImage imageNamed:@"rt_certified_fresh.png"];
}
else if ([criticsRatingString isEqualToString:@"Fresh"])
{
self.ratingImage = [UIImage imageNamed:@"rt_fresh.png"];
}
else if ([criticsRatingString isEqualToString:@"Rotten"])
{
self.ratingImage = [UIImage imageNamed:@"rt_rotten.png"];
}
else if ([criticsRatingString isEqualToString:@"Spilled"])
{
self.ratingImage = [UIImage imageNamed:@"rt_spilled.png"];
}
else if ([criticsRatingString isEqualToString:@"Upright"])
{
self.ratingImage = [UIImage imageNamed:@"rt_upright.png"];
}
[tomatorRatingImages addObject:self.ratingImage];
NSLog(@"tomatoRatingImages: %@", tomatorRatingImages);
}
但我的TomatoRatingImages的NSLog返回NULL。任何想法为什么它返回NULL?
更新:我的NSMutableArray已在我的viewWillAppear方法中初始化。
答案 0 :(得分:3)
在向对象添加对象之前,必须初始化该数组。
NSMutableArray *tomatorRatingImages = [[NSMutableArray alloc] init];
答案 1 :(得分:2)
NSMutableArray * tomatorRatingImages =[[NSMutableArray alloc]init];
[tomatorRatingImages addObject:[UIImage imageNamed:@"rt_spilled.png"]];
[tomatorRatingImages addObject:[UIImage imageNamed:@"rt_spilled1.png"]];
[tomatorRatingImages addObject:[UIImage imageNamed:@"rt_spilled2.png"]];
您可以在NSMutableArray中输入多个图像。
答案 2 :(得分:0)
像这样初始化
NSMutableArray * tomatorRatingImages =[[NSMutableArray alloc]initWithCapacity:3];
答案 3 :(得分:0)
如果您有NSMutableArray实例的属性,最好编写getter来初始化它。它只负责初始化变量,只有在需要时才加载它。
-(NSMutableArray *) tomatorRatingImages {
if(_tomatorRatingImages == nil) {
_tomatorRatingImages = [NSMutableArray alloc] init];
}
return _tomatorRatingImages;
}