Frage

How do i figure out which selector is this , from this error message ?

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayM length]: unrecognized selector sent to instance 0x178059b30'

how do I make xcode give me normal error messages, ones that will tell me where the problem is exactly?

War es hilfreich?

Lösung

That is a normal error message. The system can't tell you more than this.

But with the help of the debugger (exception breakpoints enabled), and a good understanding of Objective-C you should be able to diagnose the problem.

Here is what we know from the exception message:

  1. The unrecognized selector was length. The most common class that has this method is NSString.
  2. The object that received this method was a NSArray
  3. After the exception breakpoint was triggered type po 0x178059b30 into the debugger to inspect the array. This content of the array can give you hints to figure out how the problem was caused initially.
  4. From the debugger position in your code you can see what triggered the bug.

You say your code failed at [_label setText:name].

This method expects a NSString, during assignment a couple of NSString specific methods are called, length is one of these methods. Which confirms point one of our guess.

There are two possibilities:

  1. name is actually a NSArray because you wrote code that assigned the wrong object. This often happens if you don't check the results you get from a dictionary or an array.
  2. You have a memory problem and name was deallocated prematurely and an array is now where the NSString should be.

To rule out the second option a run of the Analyzer is often enough. It will point out most memory problems.

Another way to decide which of the two is more likely you use the output of po 0x178059b30. If the array holds objects that are related to the expected value of name (e.g. it actually contains name) there is a high chance that you did assign the wrong object. In this case look where you set name, and put a check around it.

If you use a property you can use something like this to figure out where you assign the wrong object:

- (void)setName:(NSString *)name
     _name = [name copy];
     if (![name isKindOfClass:[NSString class]]) {
         NSAssert(NO, @"Wrong class %@", name);     
     }
}

A breakpoint will be triggered when the assigned object is not a NSString instance, and you can inspect the call stack to see where you did something wrong.
Make sure to remove that code after you are done debugging. You MUST NOT use such checks to prevent bugs in your code.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top