Frage

I am using static analyzer for the first time and having difficulty to figure out the arrows. After looking some similar questions on S.O, I think the problem is the CGSize size is nil value but I am not entirely sure how its working.

Here's 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

War es hilfreich?

Lösung

  1. CGSize is a C struct
  2. [self keyboardSize:notification] may return nil

When declaring a C struct, its values have garbage values. That is, whatever was in that piece of memory before. If your call to keyboardSize returns an uninitialized CGSize, that C struct will have what's called "garbage value".

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

CGSize keyboardSize = CGSizeMake(0, 0);

Andere Tipps

You have a missing else condition.

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 handling the missing else condition or initializing keyboardSize.

CGSize keyboardSize = CGSizeZero;
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top