【问题标题】:Value stored to 'image' during its initialization is never read在初始化期间存储到“图像”的值永远不会被读取
【发布时间】:2011-12-15 09:11:12
【问题描述】:

在 xcode 4.2 中运行分析时,我收到警告“在初始化期间存储到 'image' 的值永远不会被读取。”任何人都可以帮助我解决代码有什么问题吗?

UIImage *image=[[[UIImage alloc] init]autorelease];
if (carousel==recipeCarousel) {
    image = [recipeItems objectAtIndex:index];
} else {
    image = [packItems objectAtIndex:index];
}    
UIView *view = [[[UIImageView alloc] initWithImage:image] autorelease];
return view;

谢谢。

【问题讨论】:

    标签: memory-leaks memory-management


    【解决方案1】:

    你已经用这段代码分配了 UIImage

    UIImage *image=[[[UIImage alloc] init]autorelease];
    

    但稍后您在recipeItemspackItems 中分配对象

    这样你就会失去对分配对象的控制。

    所以你可以这样做

    UIImage *image = nil;
    
    if (carousel==recipeCarousel) {
        image = [recipeItems objectAtIndex:index];
    } else {
        image = [packItems objectAtIndex:index];
    }    
    

    NSArray *targetItems = nil;
    
    if (carousel==recipeCarousel) {
        targetItems = recipeItems;
    } else {
        targetItems = packItems;
    }    
    
    UIImage *image = [targetItems objectAtIndex:index];
    

    【讨论】:

      猜你喜欢
      • 2011-08-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-11
      • 1970-01-01
      • 2018-07-26
      • 2015-07-26
      • 2012-01-19
      相关资源
      最近更新 更多