The right operand '-' is the value for garbage

I am using a static analyzer for the first time and can hardly detect the arrows. Having looked at some similar questions on SO, I think the problem is that the size of CGSize is zero, but I'm not quite sure how it works.

Here is the code:

- (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; } 

enter image description here

+4
source share
2 answers
  • CGSize is a C struct
  • [self keyboardSize:notification] may return nil

When declaring a structure C, its values ​​have the meaning of garbage. That is, whatever was in this part of the memory before. If your call to keyboardSize returns an uninitialized CGSize , then C struct will have what is called a "garbage value".

Now that I see your CGSize implementation, change the declaration of the keyboardSize variable in your keyboardSize method to:

 CGSize keyboardSize = CGSizeMake(0, 0); 
+6
source

You have a condition for the absence of else.

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

You can fix this by processing the else condition or by initializing keyboardSize.

 CGSize keyboardSize = CGSizeZero; 
+1
source

All Articles