Im trying to use the NSLog, to print console messages. The problem is sometimes i receive a "EXC_BAD_ACCESS" error when calling it
-(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
NSLog(@"Working test %d", toInterfaceOrientation);
NSLog(@"EXC_BAD_ACCESS %@", toInterfaceOrientation);
}
Here i simply want to see what the arguments passed int开发者_高级运维o the function contain. The first NSLog works fine. The second causes an "EXC_BAD_ACCESS" and i dont understand why?.
%@
only works with objects. And toInterfaceOrientation is not an object.
As you can see in the documentation for UIInterfaceOrientation
it's just an enum.
The second NSLog crash because you try to print an integer as a NSObject (%@ instead of %d). UIInterfaceOrientation is a enum it doesn't work.
EXC_BAD_ACCESS usually means you're trying to call an object that's been released from memory. Try turning on NSZombies in your environment variables to see where it's causing the problem
Answer in a similar question here: How to use NSzombie in xcode?
http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/Strings/Articles/formatSpecifiers.html
%@ is for objects only.
UIInterfaceOrientation
is an enum:
http://developer.apple.com/library/ios/#documentation/uikit/reference/UIDevice_Class/Reference/UIDevice.html#//apple_ref/doc/c_ref/UIDeviceOrientationPortrait
When you use %@ it is basically calling:
[UIInterfaceOrientation descriptionWithLocale]
Obviously this will cause a EXC_BAD_ACCESS
toInterfaceOrientation is a enum variable... so if you want to print log of it you have to use %d ...... . and %@ mostly used for objects ...
Use this Code :
NSLog(@"EXC_BAD_ACCESS :%d",toInterfaceOrientation);
EXC_BAD_ACCESS means your code is pointing to inaccessible memory address.
Such as:
- Make a pointer point to an invalid memory address
- Write to read-only memory
- Jump to an instruction at an invalid memory address
- Access a released object's property(send message to released object)
Your second line code causes a crash because of "%@". iOS thinks that the pointer you sent to it is an object, it access the property of that object, which doesn't exist, then it throws the EXC_BAD_ACCESS.
reference: Apple's Investigating Memory Access Crashes
精彩评论