problem with NSManagedObject - iphone

I have a core data NSManagedObject named CD_CoffeeShop . When i allocated a variable
CD_CoffeeShop *temp_CS = [ CD_CoffeeShop new]
and try to pass a value to it,
[temp_CS setCoffeeShopId:[NSNumber numberWithInt:coffeeShop.Id]];
(the variable coffeeShopId of temp_CS is a NSNumber)
i get an error that says :
Terminating app due to uncaught exception
'NSInvalidArgumentException', reason: '-[CD_CoffeeShop
setCoffeeShopId:]: unrecognized selector sent to instance 0x6b34040'
What can it be ?

Have you read the Core Data Programming guide? You do not instantiate core data objects with the NSObject methods. You either obtain them with a fetch request if they exist already or you use NSEntityDescription to insert a new object into the context.
Definitely don't use new.

Related

JSON to core-data

I am trying to save JSON response to local core-data for my app. I am following the following implementation for this:
https://stackoverflow.com/a/2363996/127036
Here is my code:
NSString *objectName = #"Post";
NSManagedObject *managedObject = [NSEntityDescription insertNewObjectForEntityForName:objectName inManagedObjectContext:moc];
[managedObject setValuesForKeysWithDictionary:structureDictionary];
Compiler returns following error:
-[__NSCFDictionary entity]: unrecognized selector sent to instance 0x6dc6fa0
While trying to execute:
setValuesForKeysWithDictionary
Please guide me in the right direction to fix this issue.
I'm not seeing setValuesForKeysWithDictionary in the dev center for NSManagedObject
http://developer.apple.com/library/ios/#documentation/Cocoa/Reference/CoreDataFramework/Classes/NSManagedObject_Class/Reference/NSManagedObject.html
You may have to loop through and do them one at a time with setValue:forKey:
This post is quite old now and I'm sure the Vibhor Goyal has already moved on. But in case anyone runs into this same problem, the reason why he's getting
-[__NSCFDictionary entity]: unrecognized selector sent to instance
Is because in his Post model, I'm sure he has sub entities, probably something like "comments" which is probably a to-many relationship to a "Comment" model.
Then in his structureDictionary, he probably has the usual key-value fields that corresponds to his Post model, and for the "comments" key he probably has a NSSet of NSDictionaries for the value.
So the reason why he's getting that entity selector error is because under the hood, core data is expecting "comments" to be a set filled with instances of "Comment" which are supposed to be instances of NSManagedObject, but instead it's getting instances of NSDictionary. So when it asks for "entity" from each of the "comments" it throws the unrecognized selector error.
Hope this helps someone in the future.

Iphone 'NSInvalidArgumentException', reason: '-[__NSCFSet setPolicyNumber:]

Hi i am a newbie to iPHone development and I am getting this error that states that the setter method cannot be found for a string variable despite that variable being declared with the #property and #synthesize declarations. The variable is contained within a seperate object to the class which I am attempting to set the value.
Any help would be appreciated.
here is the code relating to the error.
[reg setPolicyNumber:self.policyNoField.text];
[reg printSummaryToConsole];
debugger says that
-[__NSCFSet setPolicyNumber:]: unrecognized selector sent to instance 0x643fd80
* Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFSet setPolicyNumber:]: unrecognized selector sent to instance 0x643fd80'
I have allocated an initialized the Registration class (reg) in this class and I have the PolicyNumber created and synthesized in the registration class also. yet I am still getting this error.
Any help would be appreciated.
reg is a reference to an instance of NSCFSet.
Most likely, you failed to retain whatever reg was supposed to refer to in the first place and, coincidentally, an NSCFSet happened to be allocated where the old object was.
Use Build and Analyze, then use zombie detection if that doesn't identify the problem.

Using "replaceObjectAtIndex" for array within array

I have an array within an (mutable) array. I am trying to replace certain objects with "replaceObjectAtIndex."
I have tried:
[[mutableArrayName objectAtIndex:0]replaceObjectAtIndex:0 withObject:#"TEST"];
but I get the following error:
-[__NSArrayI replaceObjectAtIndex:withObject:]: unrecognized selector sent to instance 0x4e24d70
2011-03-17 17:02:07.008 Contact details[5145:207] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayI replaceObjectAtIndex:withObject:]: unrecognized selector sent to instance 0x4e24d70'
I tried this as well:
[mutableArrayName replaceObjectAtIndex:[[mutableArrayName objectAtIndex:0]objectAtIndex:0] withObject:#"TEST"];
but I get the following error:
* Terminating app due to uncaught exception 'NSRangeException', reason: '* -[NSMutableArray replaceObjectAtIndex:withObject:]: index 16660 beyond bounds [0 .. 0]'
The 2nd approach relies on having 3 arrays instead of 2. 1st approach seems to be fine but I guess you have an NSArray inside of an NSMutableArray because NSArray:replaceObjectAtIndex:withObject does not exist. So ensure that all arrays are mutable.
So as far as I've understood, you have: a mutable array, and within that, you've got more arrays. Now you want to fetch one of those "sub-arrays" and modify it.
In this case the first try is the correct one, except that you've got NSArray instances inside your NSMutableArray. And you cannot modify those, hence the exception. So you need to make sure you're stuffing NSMutableArrays inside your outer NSMutableArray. Then the call of your first try will succeed.

CoreData: NSManagedObject does not respond to user defined message

Using CoreData (on an iPhone app) I generated my entity classes from the model and I added some more methods to some ones. It appears that sometimes I get an exception for calling one of those methods. The exception is not random but concerns only some ManagedObject subclass (the others seem to respond correctly).
Here is an example of what i get:
-[NSManagedObject printTime]: unrecognized selector sent to instance 0x5b50af0
2010-07-15 10:29:55.216 LP[6686:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSManagedObject printTime]: unrecognized selector sent to instance 0x5b50af0'
The NSManagedObject is an object I get from a fetch (casted to the correct subclass), and the methods I am talking about are printing methods. (I tried to retrieve those not as faults either)
Am I missing something?
Did you set the correct class for the entity in the managed object model?

Unrecognized selector sent to instance - on setter method

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
)
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]]
'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?