xcode 如何将 UIImage 添加到 NSMutableArray

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14867154/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 02:46:05  来源:igfitidea点击:

How to Add UIImage to NSMutableArray

objective-cxcodeuiimagensmutablearray

提问by jaytrixz

I'm trying to add my UIImagefrom web service to my NSMutableArray using this code:

我正在尝试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);
}

But my NSLog for tomatoRatingImages returns NULL. Any ideas why it returns NULL?

但是我的用于tomatoRatingImages 的NSLog 返回NULL。任何想法为什么它返回NULL?

UPDATE: my NSMutableArray is already initialized in my viewWillAppear method.

更新:我的 NSMutableArray 已经在我的 viewWillAppear 方法中初始化。

回答by endy

You must initialize the array before adding objects to it.

您必须在向数组添加对象之前初始化数组。

NSMutableArray *tomatorRatingImages = [[NSMutableArray alloc] init];

回答by SanjayPatel

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"]];

you can enter multiple image in NSMutableArray.

您可以在 NSMutableArray 中输入多个图像。

回答by Shashank

If you have a property for the NSMutableArray instance, it's better to write getter to initialize it. It takes care of initializing the variable only ones also it gets loaded only when it's required.

如果您有 NSMutableArray 实例的属性,最好编写 getter 来初始化它。它只负责初始化变量,也仅在需要时才加载。

   -(NSMutableArray *) tomatorRatingImages {
         if(_tomatorRatingImages == nil) {
               _tomatorRatingImages = [NSMutableArray alloc] init];
         }
         return _tomatorRatingImages;
    }

回答by Lithu T.V

Initialize like this

像这样初始化

NSMutableArray * tomatorRatingImages =[[NSMutableArray alloc]initWithCapacity:3];