【问题标题】:Error: Variable-sized object may not be initialized. But why?错误:可变大小的对象可能未初始化。但为什么?
【发布时间】:2011-09-26 21:32:18
【问题描述】:
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`

错误:可变大小的对象可能未初始化。但为什么呢?

【问题讨论】:

    标签: iphone memory-management


    【解决方案1】:
    UIImage *img[i] = [UIImage imageNamed:imageName];
    

    这声明了一个大小为i 的C 样式数组,并尝试使用UIImage 的实例对其进行初始化。那没有意义。你想做什么?您的其余代码在哪里?

    编辑:

    好的,我想我明白你在做什么了。只需摆脱您拥有的所有地方[i]。在循环内部,您一次只处理一项,即使您没有处理,也不是您使用数组的方式。

    【讨论】:

      【解决方案2】:

      你可能想试试这个:

      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];
      }
      

      您不需要使用 img[i] 来使用 UIImageView 填充滚动视图。

      【讨论】:

        猜你喜欢
        • 2017-03-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-02-21
        相关资源
        最近更新 更多