How to read 'unrecognized selector' error logs in obj-c / iphone apps
i've got the following crash log error in my app:
-[NSNull length]: unrecognized selector sent to instance 0x194adc8
How do i read this? Does it mean that:
- I sent a 'length' message to a '
NSNull
' object? - The 'length' method in the '
NSNull
' class crashed when trying to call a selector on another class?
Also, if it is the former option, how can i get the stack trace to see which function caused this crash? That top line is the o开发者_JAVA技巧nly error in my log.
Thanks
Door 1
NSNull
does not respond to length
You can check the documentation for NSNull to see that this is the case.
Without having an idea of what your code base is doing I am not sure where to look, you must be calling [NSNull null];
at some point to get the NSNull
object or you are using a framework somewhere that returns this.
It means you sent 'length' to NSNull and NSNull doesn't have a 'length' function.
Turning on NSZombies might help you (it keeps deallocated objects around so it can tell you which object you tried to access) but I think in this case you probably set an object to NSNull at some point (or it was returned from a function).
Anyway to turn on NSZombies, go to Project > Edit Active Executable > Arguments tab > Then add a variable called NSZombieEnabled and set the value to YES. Make sure you turn it off when you're done though because it can cause memory issues.
It means that you are using a Length method for calculating the string length
Like If([strText Length]>0) { //do something here---- } else { //Do somethig here-- }
So in above case- strText is NSNull then definitely a crash will accure and GDB will show a message like: [NSNull length]: unrecognized selector sent to instance
As above told already Null don't have Length method.
For rescue: check first: if ((NSNull *)strText == [NSNull null]) { return strText=@""; } This will prevent for crash for NSNULL
Zombies won't help in this case. As Paul.s said, NSNull is a valid object. You should get the stack trace when the app stops. Do you have the debugger pane open? Make sure it is. I'm pretty sure Xcode 4 always stops on exceptions with the stack trace. If for some reason you're still not seeing it, if you are at the (gdb) prompt, you can type 'bt' (backtrace) to get the trace.
Because you are coparing NSNull class to lenth
check before fo that
if ([NSString *str isKindOfClass:[NSNull null]]){
str=@"";
}
then check length for the str.
it works!thx
精彩评论