Exception while loading images



I’m getting an exception while loading the images after I closed and reopened the application.

2010-06-17 16:30:23.630 Homepwner[645:207] *** -[__NSCFDate length]: unrecognized selector sent to instance 0x1194b0 2010-06-17 16:30:23.634 Homepwner[645:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[__NSCFDate length]: unrecognized selector sent to instance 0x1194b0' 2010-06-17 16:30:23.638 Homepwner[645:207] Stack: ( 861696817, 860329709, 861700631, 861203093, 861166272, 845965481, 19593, 18959, 17765, 835168443, 835167651, 835095119, 835094555, 11747, 835313871, 835593059, 845721331, 861449139, 861447005, 861059891, 861060063, 834770799, 834765939, 8869, 8784 ) terminate called after throwing an instance of 'NSException'
I tracked it down to - (UIImage *)imageForKey:(NSString *)s - result = [UIImage imageWithContentsOfFile:pathInDocumentDirectory(s)]; , but I don’t know to interpret the exception.


The exception says “You are sending the message length to an instance of type __NSCFDate. It doesn’t have the method length.”

That means, most likely, the object you are passing to imageForKey: is in fact an NSDate object and not an NSString as you expect.

Remember that the variable used to point at an object doesn’t determine the type of that object - check for where you instantiate the imageKey. I bet you are encoding the date for the imageKey in encodeWithCoder: in Possession.m.


Hi Joe,

you where right on target!

I could solve my problems and errors that I had so far by myself, but this one I had no idea. This helps me to understand the exceptions better.



Those can be pretty difficult to track down sometimes. I forgot if we mentioned it in the book, but there is a switch you can toggle to have the debugger break on Objective-C exceptions. (If we didn’t mention it, it’ll be in the next version for sure.)

In the Run menu, select “Stop on Objective-C Exceptions.” Whenever your code throws an exception, the debugger will pop up and show you where it went down. Then you have to figure out why that exception is happening - unrecognized selector is a pretty easy one, since you know you are sending a message to an object that doesn’t support that method.

But, sometimes, you have to do some digging to find out why that object isn’t what you expect it to be. In this case, it was rather straightforward - the object, when instantiated, was the wrong type. However, occasionally you can get nasty exceptions when you prematurely deallocate an object and another random object takes the same spot in memory. You’ll see unrecognized selector because the object currently occupying that spot in memory doesn’t support the message, but all of your code (except the over release somewhere obscure) tells a different story. Those are not fun to track down. :slight_smile: