views:

115

answers:

3

Hello, why can´t I fill my NSArray? Where is my mistake? He always just fill one object/image to the nsarray. I set a NSlog to check which value string has and he shows me all the 20 urls.

for (int i = 0, count = [bild count]; i < count; i = i++) {
    NSString * string = [bild objectAtIndex:i];
    NSURL *url = [NSURL URLWithString:string];
    NSData *datas = [NSData dataWithContentsOfURL:url];
    UIImage *img = [[UIImage alloc] initWithData:datas];
    myArray = [NSMutableArray array];      // this will autorelease, so if you need to keep it around, retain it
    [myArray addObject:img];
    int count = [myArray count];
    NSLog(@"There are %d elements in my array", count);
}

Thanks for your help!

+5  A: 

everytime through your loop you are creating a new aray

 myArray = [NSMutableArray array];  
darren
Ah great now it works" :) It was a stupid mistake^^
Flocked
+2  A: 

Each time through the loop, you're re-initializing your array. Move the array declaration out of the loop (before it) to fix this.

Ben Gottlieb
+1  A: 
  1. Create the NSMutable array outside the loop
  2. Call addObject on the NSMutableArray
Mr-sk