views:

516

answers:

2

Every time try to set the value of any variable in my model object, I receive 'Unrecognized selector sent to instance' error, and the app crashes. The ivars have been synthesized and they are not readonly. I have checked to see the values set are of the right type.

I am not sure if it has to do with some connection in IB, which I have checked an re-checked.

One extra bit of information: I started developing in an earlier version of Xcode, and the same piece of code used to work on that version.

Here is the exact error message:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSCFString setDistance:]: unrecognized selector sent to instance 0x380ce50'
2009-11-10 15:10:58.113 CabMeter[7432:207] Stack: (
    29303899,
    2457931593,
    29685819,
    29255286,
    29107906,
    11415,
    3140002,
    3149770,
    3199319,
    3236748,
    3170686,
    3230561,
    3179329,
    12452,
    3918761,
    3933474,
    4979284,
    4987529,
    3990121,
    2838067,
    2746396,
    2773173,
    37400273,
    29088640,
    29084744,
    37394317,
    37394514,
    2777091,
    9208,
    9062
)
A: 

'Unrecognized selector sent to instance' sounds like you have a delegate the defines a selector for a method that doesn't exist.

Do you have a @selector(methodName) anywhere in your code?

In the stack trace, what is the last of your code that is called before the exception is thrown?

If it's on the setter method, could we see how you declare the instance variable and how you synthesize it?

Ben S
+5  A: 

The ivars have been synthesized and they are not readonly.

I think you're confusing instance variables with properties. Properties are what you normally synthesize and/or make read-only, and they are usually, but not necessarily, backed by instance variables.

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSCFString setDistance:]: unrecognized selector sent to instance 0x380ce50'

This is why you should read the error message.

You're not sending your setDistance: message to your model object—you're sending it to a string. Most likely, you didn't retain the model object like you should have, and a string got allocated shortly thereafter with the same address.

Review the memory management rules and look to find where you're not following them. If all of your properties are set correctly, make sure you're actually using them: A common mistake is to assign directly to the ivar:

myModel = [[[MyModel alloc] init] autorelease]

instead of going through the property:

self.myModel = [[[MyModel alloc] init] autorelease]
or
[self setMyModel:[[[MyModel alloc] init] autorelease]]

Peter Hosey
Spot on! I had not retained the model, and the memory location had been occupied by a string, dictionary or something else at various times! Thanks a ton..
Prasanna