How do I use addObject within an Array?

Asked

Viewed 107 times

1

I have a Nsmutablearray *arrayFavoritos

In the log I can see

{
        name1 = 6;
        name2 = "Paolo Rossi\nIt\U00e1lia";
        name3 = "cartaz-1982.jpg";
        photo = "copa8.jpg";
        name4 = 24;
    },
        {
        name1 = 6;
        name2 = "Oleg Salenko\nR\U00fassia";
        name3 = "cartaz-1994.jpg";
        photo = "copa5.jpg";
        name4 = 24;
    },

I need to transform that information:

self.tableItems = @[[UIImage imageNamed:@"photo1.jpg"],
                        [UIImage imageNamed:@"photo2.jpg"],
                        [UIImage imageNamed:@"photo3.jpg"]];

So that this array be dynamically mounted

How can I do that?

  • Do you have an array of objects in the right first code? What is the value of the object that interests you to compose the tableItems?

1 answer

0


If I understand correctly, you need to generate a vector of Uiimage objects based on another vector that contains dictionaries whose relevant key is called photo. In this case, you can simply traverse the original vector, extract the string corresponding to the photo key and use it to build an Uiimage-like object by adding it to another vector:

NSMutableArray photos = [NSMutableArray array];
// arrayFavoritos contém elementos de tipo NSDictionary…
for (NSDictionary *photoData in arrayFavoritos) {
    // …e cada dictionary contém um par chave-valor com chave photo…
    NSString *photoName = photoData[@"photo"]; // nil caso não exista
    if (photoName != nil) {
        // …que é usada para construir um objeto UIImage
        [photos addObject:[UIImage imageNamed:photoName]];
    }
}
self.tableItems = photos;

If you want to extract only the names of the photos, without building Uiimage objects, you can use KVC:

NSArray *photoNames = [arrayFavoritos valueForKey:@"photo"];

The photoNames array will either contain or strings with the photo value for each element of the photoNames array, or [NSNull null] for cases where there is no key called photo.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.