1

我正在使用 Autoscroll 示例中的 Apple TapDetectingImageView 类。

静态分析器显示以下警告:

类/TapDetectingImageView.m:68:30:{68:17-68:29}:警告:

The left operand of '==' is a garbage value
         if (tapCounts[0] == 1 && tapCounts[1] == 1) {
             ~~~~~~~~~~~~ ^

对于下面的附加代码。垃圾值发生在没有先初始化的情况下读取变量。但似乎 tapCounts 已经初始化。

如果应用程序运行良好,我可以忽略它还是应该修改任何内容?

    BOOL allTouchesEnded = ([touches count] == [[event touchesForView:self] count]);

// first check for plain single/double tap, which is only possible if we haven't seen multiple touches
if (!multipleTouches) {
    UITouch *touch = [touches anyObject];
    tapLocation = [touch locationInView:self];

    if ([touch tapCount] == 1) {
        [self performSelector:@selector(handleSingleTap) withObject:nil afterDelay:DOUBLE_TAP_DELAY];
    } else if([touch tapCount] == 2) {
        [self handleDoubleTap];
    }
}   

// check for 2-finger tap if we've seen multiple touches and haven't yet ruled out that possibility
else if (multipleTouches && twoFingerTapIsPossible) {

    // case 1: this is the end of both touches at once
    if ([touches count] == 2 && allTouchesEnded) {
        int i = 0;
        int tapCounts[2]; CGPoint tapLocations[2];
        for (UITouch *touch in touches) {
            tapCounts[i]    = [touch tapCount];
            tapLocations[i] = [touch locationInView:self];
            i++;
        }
        if (tapCounts[0] == 1 && tapCounts[1] == 1) { // it's a two-finger tap if they're both single taps
            tapLocation = midpointBetweenPoints(tapLocations[0], tapLocations[1]);
            [self handleTwoFingerTap];
        }
    }
4

2 回答 2

0

这发生在创建变量但未初始化为值时。在上面的代码中,你这样做:

int tapCounts[2];
...

但是,在尝试在 if 语句中向下几行对其进行评估之前,不要将数组内容初始化为任何内容:

if( tapCounts[0] == 1 && tapCounts[1] == 1 ) {
   ....
}

编译器警告您 tapCounts[0] 的内容未初始化并且是垃圾。

于 2011-10-15T20:26:25.090 回答
0

这是因为 tapCounts 数组是用索引 0 和 1 的垃圾值初始化的。

请换行:

int tapCounts[2]; CGPoint tapLocations[2];

int tapCounts[2] = {0,0}; CGPoint tapLocations[2] = {0,0};

这将在分析构建时删除初始化警告

于 2016-02-17T17:18:40.883 回答