簡體   English   中英

' - '的右操作數是垃圾值

[英]the right operand of '-' is a garbage value

我第一次使用靜態分析儀,很難弄清楚箭頭。 在看了一些關於SO的類似問題后,我認為問題是CGSize大小是零值,但我不完全確定它是如何工作的。

這是代碼:

 - (void)keyboardDidShow:(NSNotification*)notification {
    CGSize size = CGSizeMake(0, 0);
    size = [self keyboardSize:notification];
      if (UIInterfaceOrientationIsLandscape(self.interfaceOrientation)) {
            detailTableView.frame = CGRectMake(detailTableView.frame.origin.x, detailTableView.frame.origin.y,
                                       detailTableView.frame.size.width, kTableViewMovableHeight + kTableViewDefaultHeight -  size.height
                                       );
    //detailTableView.scrollEnabled = YES;
    }
}


- (CGSize)keyboardSize:(NSNotification *)aNotification {
NSDictionary *info = [aNotification userInfo];
NSValue *beginValue = [info objectForKey:UIKeyboardFrameEndUserInfoKey];
UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
CGSize keyboardSize;
UIDeviceOrientation _screenOrientation = orientation;
if ([UIKeyboardDidShowNotification isEqualToString:[aNotification name]]) {
    if (UIDeviceOrientationIsPortrait(orientation)) {
        keyboardSize = [beginValue CGRectValue].size;
    } else {
        keyboardSize.height = [beginValue CGRectValue].size.width;
        keyboardSize.width = [beginValue CGRectValue].size.height;
    }
} else if ([UIKeyboardWillHideNotification isEqualToString:[aNotification name]]) {
    if (_screenOrientation == orientation) {
        if (UIDeviceOrientationIsPortrait(orientation)) {
            keyboardSize = [beginValue CGRectValue].size;
        } else {
            keyboardSize.height = [beginValue CGRectValue].size.width;
            keyboardSize.width = [beginValue CGRectValue].size.height;
        }
        // rotated
    } else if (UIDeviceOrientationIsPortrait(orientation)) {
        keyboardSize.height = [beginValue CGRectValue].size.width;
        keyboardSize.width = [beginValue CGRectValue].size.height;
    } else {
        keyboardSize = [beginValue CGRectValue].size;
    }
}
return keyboardSize;
}

在此輸入圖像描述

  1. CGSize是一個C結構
  2. [self keyboardSize:notification]可能返回nil

聲明C結構時,其值具有垃圾值。 也就是說,之前的那段記憶中的任何東西。 如果您對keyboardSize的調用返回未初始化的CGSize ,那么該C結構將具有所謂的“垃圾值”。

現在我看到了CGSize的實現,將keyboardSize方法中變量keyboardSize的聲明更改為:

CGSize keyboardSize = CGSizeMake(0, 0);

你有一個失蹤的其他條件。

if ([UIKeyboardDidShowNotification isEqualToString:[aNotification name]]) {
    // ...
} else if ([UIKeyboardWillHideNotification isEqualToString:[aNotification name]]) {
    // ...
}
// else not handled could result in keyboardSize not being set.
return keyboardSize;

您可以通過處理缺少的else條件或初始化keyboardSize來解決此問題。

CGSize keyboardSize = CGSizeZero;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM