How do I use addObject inside an Array?

1

I have an NSMutableArray * arrayFavorites

I can not log in

{
        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 this information:

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

For this array to be mounted dynamically

How can I do this?

    
asked by anonymous 25.05.2014 / 19:04

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 go through the original vector, extract the string corresponding to the key photo and use it to construct an object of type UIImage, 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 just want to extract the names of the photos without building UIImage objects, you can use KVC :

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

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

    
27.05.2014 / 08:01