views:

216

answers:

1

Hi,

I was following the example on Beginning iPhone 3 Development. On Chapter 8 I made a mistake in code.

    - (NSMutableDictionary *)mutableDeepCopy
{
 NSMutableDictionary * ret = [[NSMutableDictionary alloc] initWithCapacity:[self count]];

 NSArray * keys = [self allKeys];
 for (id key in keys) {
  id oneValue = [self valueForKey:key];
  id oneCopy = nil;

  if ([oneValue respondsToSelector:@selector(mutableDeepCopy)])
   oneCopy = [oneValue mutableDeepCopy];
  else if ([oneValue respondsToSelector:@selector(mutableCopy)])
   oneCopy = [oneValue mutableCopy];
  if (oneCopy == nil)
   oneCopy = [oneValue copy];
  [ret setValue:oneCopy forKey: key];
 }
 return ret;
}

In the second responseToSelector, instead of the mutableCopy above, I have mistakenly written it as mutableDeepCopy. As a result my creation of mutable array from the regular one has failed to a simple copy.

As a result console will print error message like this:

2010-02-04 19:58:28.381 Sections[1806:20b] * WebKit discarded an uncaught exception in the webView:shouldInsertText:replacingDOMRange:givenAction: delegate: * -[NSCFArray removeObjectAtIndex:]: mutating method sent to immutable object

Now my question is, this is really hard for me to debug if I'm writing my own code instead of just copying it from book. How do I know at which line this "mutating method sent to immutable object" occurs?

+1  A: 

Step 1. Use the debugger.

Run -> Debugger or Shift-Command-Y. When your program encounters an error like the one above, you can see where in the code it was stopped. You can see Apple's instructions on using the debugger for details, but basic stuff is pretty easy to figure out. The most important part is the thread list panel in the upper-left quadrant of the debugger. It'll allow you to move up and down through the stack to see where in your code the error occurred. Usually you'll be able to use this to determine which one of your objects was declared as immutable and not mutable.

Step 2. Use Instruments.

Instruments is powerful and will allow you to do some pretty nifty stuff. In this situation, once you find out the memory address if your accidentally-immutable object, you can use Instruments to see the history of that object and hopefully trace it back to it's origin. To use instruments to track an object, you'll want to run Instruments with Object Allocation (Run -> Run with Performance Tool -> Object Allocations). If you know the address of the fouled-up object, you can search for it in the lower-right corner of Instruments, in the search box. Open the Extended Detail view (Command-E) to see where that object has been.

kubi
What happens when I don't see anything in the debugger? Is it because lack of debugging symbols? How do I add them?
huggie
It's probably because you don't have any breakpoints set or because you aren't currently stopped at a breakpoint. Try setting a breakpoint at the start of some bit of code you know is called to see what it should look like.
kubi