当我在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.以下是箭头:

它想告诉我什么?代码运行得很好。
发布于 2010-10-24 20:58:52
在看不到整个函数的情况下很难判断,但我认为这意味着代码可能会在location.x永远不会初始化的地方找到一条路径。在您的测试中,代码可能永远不会走这条路,但这种可能性是存在的。
编辑:我将在这里胡乱猜测一下,这是因为touches anyObject可能会返回nil。在这种情况下,[touch locationInView:self]将返回垃圾(请记住,向nil发送消息是完全有效的)。
尝试使函数的其余部分以(touch != nil)为条件。
发布于 2013-04-03 09:23:50
'/‘的左操作数是一个垃圾值,这表明告诉我们必须给location.x一个初始值,因为touch locationInView:self可能为nil,所以必须判断if(touch!=nil)
发布于 2012-07-08 06:37:59
如果touch为零,touch locationInView:将返回nil结构结果,在某些版本的llvm之前,它只保证结构的指针字节的第一个大小被清零,其余的仍然是垃圾。
AFAIK接触到anyObject永远不能为零,并且警告是一个假阳性。
https://stackoverflow.com/questions/4008276
复制相似问题