简体   繁体   中英

What is the 'garbage value' in 'Left operand of '/' is a garbage value' warning generated by “Build & Analyze”?

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:

-(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.

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.

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).

Try making the rest of the function conditional on (touch != nil) .

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

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.

AFAIK [touches anyObject] can never be nil and the warning is a false positive.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM