xcode “构建和分析”生成的“'/'的左操作数是垃圾值”警告中的“垃圾值”是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4008276/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the 'garbage value' in 'Left operand of '/' is a garbage value' warning generated by "Build & Analyze"?
提问by Michael Forrest
When I 'Build and Analyze" this code in Xcode, I get a warning that I don't understand. Here's the method with the problem:
当我在 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];
}
Here's the warning:
这是警告:
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.
Here are the arrows:
这是箭头:
What is it trying to tell me? The code works fine.
它想告诉我什么?代码工作正常。
回答by Ferruccio
It's hard to tell without seeing the entire function, but I would take that to mean that there exists a path the code could take where location.x is never initialized. It may be that the code never takes that path in your testing, but the possibility is there.
如果没有看到整个函数,很难判断,但我认为这意味着存在代码可以采用的路径,location.x 从未被初始化。可能代码在您的测试中从不采用该路径,但可能性是存在的。
EDIT: I'm going to take a wild guess here and say that it's because [touches anyObject] could conceivably return nil
. In which case [touch locationInView:self]
will return garbage (remember sending messages to nil
is perfectly valid).
编辑:我将在这里大胆猜测并说这是因为 [touches anyObject] 可能会返回nil
。在这种情况下[touch locationInView:self]
将返回垃圾(记住发送消息到nil
是完全有效的)。
Try making the rest of the function conditional on (touch != nil)
.
尝试使函数的其余部分以 为条件(touch != nil)
。
回答by Wanhuagu
the left operand of '/' is a garbage value ,This suggests tell us that must give an initial value to location.x , because [touch locationInView:self] maybe nil,so Must make a judgment whether the if(touch!=nil)
'/' 的左操作数是一个垃圾值,这表明必须给 location.x 一个初始值,因为 [touch locationInView:self] 可能为 nil,所以必须判断 if(touch!=nil )
回答by nevyn
If touch is nil, [touch locationInView:] will return the nil struct result, which before some version of llvm would only guarantee that the first [size of pointer] bytes of the struct are zeroed, and the rest would still be garbage.
如果 touch 为 nil,[touch locationInView:] 将返回 nil struct 结果,这在某些版本的 llvm 之前只会保证结构的第一个 [size of pointer] 字节为零,其余的仍然是垃圾。
AFAIK [touches anyObject] can never be nil and the warning is a false positive.
AFAIK [touches anyObject] 永远不会为零,并且警告是误报。