0

I have a problem with this method. I have eleven images with this names: Articulo 1.png, Articulo 2.png, etc. I dont know why , but my array of images only has the last image. Am I doing something wrong?

Here is my example:

NSMutableArray imagenesA = [[NSMutableArray alloc]init];
NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
UIImage *image = [[UIImage alloc]init];
UIImageView *imageView = [[UIImageView alloc]init];
NSMutableString *name = [[NSMutableString alloc]init];
NSMutableString *name2 = [[NSMutableString alloc]init];

for (int y = 0; y < 11; y++)  {

    name = [NSMutableString stringWithFormat:@"Articulo %d.png",y+1];
    image = [UIImage imageNamed:name];
    imageView = [[UIImageView alloc] initWithImage:image];
    name2 = [NSMutableString stringWithFormat:@"Articulo %d",y+1];
    [dict  setObject:name2  forKey:@"nombre"];
    [dict  setObject:imageView forKey:@"imagen"];

    [imagenesA insertObject:dict atIndex:y];

    // show all dictionarys
    NSLog(@"DICT: %@", dict);
}

// show index 0 element --> must be Articulo 1 but...
NSDictionary *d = [[NSDictionary alloc] 
                    initWithDictionary:[imagenesA objectAtIndex:0]]; 
NSLog(@"*********************%@***********************",d);

Console response:

2011-08-20 14:26:47.411 Catalogo-V1[28155:207] DICT: { imagen = ">"; nombre = "Articulo 1"; } 2011-08-20 14:26:47.411 Catalogo-V1[28155:207] DICT: { imagen = ">"; nombre = "Articulo 2"; } 2011-08-20 14:26:47.412 Catalogo-V1[28155:207] DICT: { imagen = ">"; nombre = "Articulo 3"; } 2011-08-20 14:26:47.412 Catalogo-V1[28155:207] DICT: { imagen = ">"; nombre = "Articulo 4"; } 2011-08-20 14:26:47.413 Catalogo-V1[28155:207] DICT: { imagen = ">"; nombre = "Articulo 5"; } 2011-08-20 14:26:47.414 Catalogo-V1[28155:207] DICT: { imagen = ">"; nombre = "Articulo 6"; } 2011-08-20 14:26:47.414 Catalogo-V1[28155:207] DICT: { imagen = ">"; nombre = "Articulo 7"; } 2011-08-20 14:26:47.415 Catalogo-V1[28155:207] DICT: { imagen = ">"; nombre = "Articulo 8"; } 2011-08-20 14:26:47.415 Catalogo-V1[28155:207] DICT: { imagen = ">"; nombre = "Articulo 9"; } 2011-08-20 14:26:47.416 Catalogo-V1[28155:207] DICT: { imagen = ">"; nombre = "Articulo 10"; } 2011-08-20 14:26:47.416 Catalogo-V1[28155:207] DICT: { imagen = ">"; nombre = "Articulo 11"; } 2011-08-20 14:26:47.416 Catalogo-V1[28155:207] **********{ imagen = ">"; nombre = "Articulo 11"; }************

2
  • 1
    In the loop, you are inserting the same dictionary to the array over and over and, in every iteration, you are replacing the objects for the keys nombre and imagen. Commented Aug 20, 2011 at 12:43
  • I don't know what are you doing but that's ugly! Paid attention to the memory leaks. Commented Aug 20, 2011 at 12:44

1 Answer 1

2

Yes. @albertamg is correct in his comment above. Your for loop should look like the following.

for (int y = 0; y < 11; y++)  {

    // Other codes here

    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    [dict  setObject:name2  forKey:@"nombre"];
    [dict  setObject:imageView forKey:@"imagen"];
    [imagenesA insertObject:dict atIndex:y];
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.