[英]What is the 'garbage value' in 'Left operand of '/' is a garbage value' warning generated by “Build & Analyze”?
當我在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.
這是箭頭:
它試圖告訴我什么? 代碼工作正常。
在沒有看到整個函數的情況下很難分辨,但我認為這意味着代碼可以采用的路徑,其中location.x永遠不會被初始化。 可能是代碼從未在測試中采用該路徑,但可能存在。
編輯:我將在這里做一個瘋狂的猜測,並說這是因為[觸及任何對象]可以想象返回nil
。 在這種情況下[touch locationInView:self]
將返回垃圾(記住發送消息到nil
是完全有效的)。
嘗試使函數的其余部分成為條件(touch != nil)
。
'/'的左操作數是一個垃圾值,這表示告訴我們必須給location.x一個初始值,因為[touch locationInView:self]可能是nil,所以必須判斷是否為if(touch!= nil) )
如果touch為nil,[touch locationInView:]將返回nil結構結果,在某個版本的llvm之前,只能保證結構的第一個[指針大小]字節為零,其余的仍然是垃圾。
AFAIK [觸及任何對象]永遠不會是零,警告是誤報。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.