EXC_BAD_ACCESS when trying to get iPhone screen di

2019-02-25 18:53发布

问题:

I'm trying to make certain elements resize according to the screen dimensions when the interface orientation changes. I've read in various places (including a few answers on here) that the way to get the bounds of the screen is to use [[UIScreen] mainScreen] bounds]. So I've added this code to see how it behaves:

- (void) willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
    NSLog(@"view dimensions: (%@, %@)", [[UIScreen mainScreen] bounds].size.width, [[UIScreen mainScreen] bounds].size.height);
}

But when running it, as soon as I rotate the screen I get an EXC_BAD_ACCESS message. Is there something about the scope of the willRotateToInterfaceOrientation function that doesn't allow me to call for the screen bounds? If so, how would I go about it?

回答1:

%@ format specifier expects format parameter to be objective-c object, while what you provide is plain float. To print float numbers use %f specifier:

NSLog(@"view dimensions: (%f, %f)", [[UIScreen mainScreen] bounds].size.width, [[UIScreen mainScreen] bounds].size.height);