Error: A variable-sized object cannot be initialized. But why?

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: A variable-sized object cannot be initialized. But why?

0
source share
2 answers

You can try the following:

 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 do not need to use img [i] to populate a scrollview with a UIImageView.

+1
source
 UIImage *img[i] = [UIImage imageNamed:imageName]; 

Declares an array of size C i and tries to initialize it with an instance of UIImage . It does not make sense. What are you trying to do? Where is the rest of your code located?

Edit:

Ok, I think I see what you are doing. Just get rid of all the places you have [i] . Inside a loop, you only deal with one element at a time, and even if you haven’t done so, that’s not how you use arrays.

+2
source

Source: https://habr.com/ru/post/913077/


All Articles