1

I am getting the following error:

[__NSCFNumber length]: unrecognized selector sent to instance

I understand what the error means, but I am not able to find where it is coming from. Turning on the All Exceptions breakpoint is not helping, and it is not showing me what line and file the error is coming from. Normally it does.

Wasted 4 hours trying manual breakpoints with no luck. Any idea how I can find this?

luk2302
  • 55,258
  • 23
  • 97
  • 137
Nic Hubbard
  • 41,587
  • 63
  • 251
  • 412
  • Perhaps try commenting out blocks of code until you figure out which line(s) are responsible – danielmhanover May 31 '15 at 18:36
  • @sddhhanover - Yeah, doing that, still is being elusive. – Nic Hubbard May 31 '15 at 18:37
  • Can you share the suspected block of code? – danielmhanover May 31 '15 at 18:38
  • @sddhhanover - It is too large and specific to my project. – Nic Hubbard May 31 '15 at 18:40
  • No callstack information available? – lead_the_zeppelin May 31 '15 at 18:40
  • Also maybe @try/catch blocks? – danielmhanover May 31 '15 at 18:40
  • You could do a `NSLog(@"%@",NSStringFromClass[object class]])`on all the places where you use `object.length` or `[object length]` if there are not too many places like this... – Rok Jarc May 31 '15 at 18:43
  • @rokjarc - I am not using `object.length`, that is part of the confusion. – Nic Hubbard May 31 '15 at 18:48
  • 3
    since you spent 4 hours I assume your project is big. I would suggest something like this: Create a category method for `NSNumber` call it `length`. Add this category to all files that you think might potentially cause it. Put a breakpoint in the implementation of length. Might work. – lead_the_zeppelin May 31 '15 at 18:51
  • Auch, this probably means that there is a `NSCFNumber` (or `NSNumber`) in some payload dictionary under a key where receiver (library, framework) would expect a `NSString` (just a hunch). A lot of 3rd party code is checking for an existence of string with string.length (it will be zero for empty string or when it is nil). – Rok Jarc May 31 '15 at 18:53
  • lead_the_zeppelin's idea is great. you might want to add this category to -Prefix.pch – Rok Jarc May 31 '15 at 18:54
  • @lead_the_zeppelin - Figured it out. The API I use was changed last night. A value that used to be a string was changed to a number. Guess I need to test for this in the future. – Nic Hubbard May 31 '15 at 18:55
  • 2
    This question is very common and would not exist if your learnt how to debug code when you started programming. – pronebird May 31 '15 at 18:57
  • @Andy - That is a rude response. I never said I didn't know how to debug, it was just a very tricky one to find. – Nic Hubbard May 31 '15 at 19:00
  • See [this thread](http://stackoverflow.com/questions/8100054/no-exception-stack-trace-in-console-under-xcode-4-2-ios-5) for some ideas on how to get the stack trace. – Hot Licks May 31 '15 at 19:24
  • 3
    @NicHubbard it's not rude, this question with exactly the same crash pops up on SO twice a week at least. Stack trace and breakpoint is not tricky. This is what debugging is. – pronebird May 31 '15 at 19:51
  • @Andy - The question was not about what the error was. It was about why the `All Exceptions` breakpoint was not finding the exception. – Nic Hubbard May 31 '15 at 19:52

1 Answers1

2

I figured it out. The API that I am using changed a string value to a number, so an NSString that I had for one of my objects was getting set to an NSNumber instead of a string. Then, later in my code that was trying to be set to a label. This caused the crash.

Nic Hubbard
  • 41,587
  • 63
  • 251
  • 412