0
enter code hereint quantity = [array count];
int i;
for (i=0; i<quantity; i++) 
{
    NSString *imageName = [NSString stringWithFormat:@"Car_%@.jpg",  [[array objectAtIndex:i] objectForKey:@"CarName"]]   ];
    UIImage *img[i] = [UIImage imageNamed:imageName];
    UIImageView *imgView[i] = [[UIImageView alloc] initWithImage:img[i]];
    imgView[i].frame = CGRectMake(i*kWidth, 0, kWidth, kHeight);
    [scrollView addSubview:imgView[i]];
    [imgView[i] release];
}`enter code here`

Error: Variable-sized object may not be initialized. But why?

Voloda2
  • 12,359
  • 18
  • 80
  • 130

2 Answers2

2
UIImage *img[i] = [UIImage imageNamed:imageName];

This declares a C-style array of size i and attempts to initialise it with an instance of UIImage. That doesn't make sense. What are you trying to do? Where is the rest of your code?

Edit:

Okay, I think I see what you are doing. Just get rid of all the places you have [i]. Inside the loop, you are only dealing with one item at a time, and even if you weren't, that's not how you use arrays.

Jim
  • 72,985
  • 14
  • 101
  • 108
1

You may want to try this:

int i;
for (i=0; i<quantity; i++) 
{
    NSString *imageName = [NSString stringWithFormat:@"Car_%@.jpg",  [[array objectAtIndex:i] objectForKey:@"CarName"]]   ];
    UIImage *img = [UIImage imageNamed:imageName];
    UIImageView *imgView = [[UIImageView alloc] initWithImage:img];
    imgView.frame = CGRectMake(i*kWidth, 0, kWidth, kHeight);
    [scrollView addSubview:imgView];
    [imgView release];
}

You don't need to use img[i] in order to populate a scrollview with UIImageView.

Saikat
  • 2,613
  • 1
  • 22
  • 14