【问题标题】:What is the 'garbage value' in 'Left operand of '/' is a garbage value' warning generated by "Build & Analyze"?“构建和分析”生成的“'/'的左操作数是垃圾值”警告中的“垃圾值”是什么?
【发布时间】:2010-10-24 12:53:19
【问题描述】:

当我在 Xcode 中“构建和分析”此代码时,我收到一个我不理解的警告。这是有问题的方法:

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
        UITouch * touch = [touches anyObject];
        CGPoint location = [touch locationInView:self];
        CGPoint relativePosition = CGPointMake(1.0-(location.x / self.bounds.size.width),location.y / self.bounds.size.height);
        [[Stage getSharedStage] movePartToLocation:relativePosition];
}

这是警告:

 warning: The left operand of '/' is a garbage value
         CGPoint relativePosition = CGPointMake(1.0-(location.x / self.bounds.size.width),location.y / self.bounds.size.height);
                                                     ~~~~~~~~~~ ^
1 warning generated.

以下是箭头:

它想告诉我什么?代码运行良好。

【问题讨论】:

  • 请粘贴一张图片,显示箭头如何指向那​​里。

标签: objective-c cocoa-touch xcode ios


【解决方案1】:

如果没有看到整个函数就很难判断,但我认为这意味着存在代码可以采用的路径,其中 location.x 永远不会被初始化。可能代码在你的测试中从来没有走这条路,但这种可能性是存在的。

编辑:我将在这里大胆猜测并说这是因为 [touches anyObject] 可以想象返回nil。在这种情况下[touch locationInView:self] 将返回垃圾(请记住向nil 发送消息是完全有效的)。

尝试使函数的其余部分以(touch != nil) 为条件。

【讨论】:

  • 我也是这么想的。什么是触摸定义?
  • 我已经展开以展示整个方法。这可能与使用 [touches anyObject] 有关吗?
【解决方案2】:

如果 touch 为 nil,[touch locationInView:] 将返回 nil 结构结果,在某些版本的 llvm 之前,它只会保证结构的第一个 [指针大小] 字节为零,其余的仍然是垃圾。

AFAIK [touches anyObject] 永远不能为零,警告是误报。

【讨论】:

    【解决方案3】:

    '/'的左操作数是垃圾值,这建议告诉我们必须给location.x一个初始值,因为[touch locationInView:self]可能为nil,所以必须判断if(touch !=无)

    【讨论】:

      猜你喜欢
      • 2022-06-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-05
      • 1970-01-01
      相关资源
      最近更新 更多