JSON to core-data - iphone

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.

Related

NSArray from NSSet of objects crashes when calling 'allObjects'

I have an core data NSSet of objects through a relationship that I am trying to put into an array.
When I call the 'allObjects' selector I get a crash on my object in the NSSet that the selector does not exist. Well, it obviously doesn't, but how do I get my data to copy into the array?
Heres the code that crashes.
NSArray *items = [surveyCategory.surveyQuestions allObjects];
Inside my surveyQuestions NSSet are SurveyQuestion objects. And this throws the error `[SurveyQuestion allObjects]: unrecognized selector sent to instance
Any ideas on how to fix this?
thanks
From the error message it looks as if surveyQuestions is not a to-many relationship
as you expect, but a to-one relationship, so that surveyCategory.surveyQuestions
is a SurveyQuestion object and not a set.
Perhaps you changed the Core Data model and did not re-create the managed object subclass
file, so that the compiler did not complain.

Xcode 4.3.3 -[__NSArrayM length]: unrecognized selector sent to instance 0x6b6dc60

I have an iPhone application where i download information from the internet and put it into an NSString. It works fine, until i download large files and put it into that one string, then i get the error
-[__NSArrayM length]: unrecognized selector sent to instance 0x6b6dc60
At one point i was getting a EXC_BAD_ACCESS error, but now that's not showing up. I'm guessing that it is a memory problem but i dont know how to fix it. Is there a limit to how large a string variable can be? Any suggestions? I should also mention that the error sometimes doesn't show up, but most of the time it does. Thanks in advance.
Well, it looks like you're trying to get the length of the array by calling a method called length on an array, but to get the length of an array you use the count method like this for example:
NSInteger numberOfElements = [someArray count];
Hope this helps!
P.S. The length method exists but it is used on NSString objects to get the number of characters in the string.
----UPDATE-----
From Ray Wenderlich's "My App Crashed, Now What?" tutorial:
The error message “unrecognized selector sent to instance XXX” means that the app is trying to call a method that doesn’t exist.
So somewhere in your code, you are calling the length method on an object of type NSArray.
You are actually calling the length method on an object of type NSMutableArray, and you know that from the error because __NSArrayM represents an NSMutableArray object; a regular NSArray object would be represented as __NSArrayI (the suffixed "M" stands for "mutable" while the suffixed "I" stands for "immutable").
I even found a very similar question that has a very similar answer to mine:
NSArrayM length : unrecognized selector sent to instance
Heh, I had this before. Somewhere you're doing this: [array length]; but arrays use "count", not "length".
This is happening because you are trying to cast NSMutableArray to NSString, you can remove this error simply by using objectAtIndex:0.
when we use objectAtIndex:0 ,it returns object and in your case that object is your NSString & hence removes error.
e.g.
self.quizTextView.text=[questionTexts objectAtIndex:0];
Try turning on zombies to get a better error message. It's probably something being deallocated before you expect. You can also try running your app in the "Allocations" or "Leaks" instrument to track down why your objects are being released before you expect.

CoreDataGeneratedAccessor method giving "unrecognized selector sent to instance" error

When i call the method:
- (void)removeObjectFromMediaAtIndex:(NSUInteger)idx;
which is one of the default methods in a file created as a core data object, i'm getting an error of unrecognized selector sent to instance. Anybody know why this might be happening?
Ensure that your NSManagedObject sublcass instance was created using an NSManagedObjectContext and not directly. Instead of leveraging #synthesize for properties, NSManagedObject sublcasses leverage the #dynamic keyword which indicates the accessors will be created at runtime - in this case, by the NSManagedObjectContext. They will not be there if you create the object instance using something like alloc]init];
It is a notorious Core Data bug. It is almost 2-year old but sadly it is still there. See this post: Exception thrown in NSOrderedSet generated accessors.
It sounds like you may have altered your data model without altering the classes, or vice-versa. Or perhaps one of your team members did (my team quickly learned about this danger). Another possibility is that the reference you are using is not actually the class you think it is. Sometimes if you overrelease an object, another object will occupy the previous memory space but it will not be the correct class.
However, this doesn't look like a default method. The default methods I am used to seeing are add object, remove object, change to a new NSSet, and one more that I can't quite remember off the top of my head. However, if you got the CoreData object to use an NSArray instead it would make sense.

Cocoa-Touch, Core Data: Getting exception when trying to insertNewObjectForEntityForName:inManagedObjectContext:

I'm following this how-to to implement Core Data storage in my app:
I have a Model.xcdatamodel which defines a Something model. I've used XCode to generate a class for that model.
I've imported the class in my .m file where I'm trying to:
Something* s = (Something *)[NSEntityDescription insertNewObjectForEntityForName:#"Something" inManagedObjectContext:managedObjectContext];
But this causes the following error:
2009-10-13 21:18:11.961 w9a[4840:20b] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '+entityForName: could not locate an NSManagedObjectModel for entity name 'Something''
Am I missing something?
Personally, I prefer the following method:
// With some NSManagedObjectContext *context
NSEntityDescription *desc = [NSEntityDescription entityForName:#"Something"
inManagedObjectContext:context];
Something *s = [[[Something alloc] initWithEntity:desc
insertIntoManagedObjectContext:context] autorelease];
I've noticed it's less prone to random Core Data errors, and is easier to debug. It's effectively doing the same thing as your code, but explicitly gets an entity description first, so you can debug that separately if need be.
Seems that you dont have a NSManageObject named "Something" in your object model...are you making your entity in the object model? I am not sure if you need to generate the code as well, but you can have xcode do that for you automatically by clicking on the entity, saying new, and selecting Managed Object from the menu there
Found my problem, the NSManagedObjectContext was not geting initialized properly for some reason. I've re-written that code following the how-to and now it seems to work.
Thanks anyway :)

Iphone Core Data Internal Inconsistency

This question has something to do with the question I posted here: Iphone Core Data crashing on Save
however the error is different so I am making a new question. Now I get this error when trying to insert new objects into my managedObjectContext:
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException',
reason: '"MailMessage" is not a subclass of NSManagedObject.'
But clearly it is:
#interface MailMessage : NSManagedObject { ....
And when I run this code:
NSManagedObjectModel *managedObjectModel = [[self.managedObjectContext
persistentStoreCoordinator] managedObjectModel];
NSEntityDescription *entity =[[managedObjectModel entitiesByName]
objectForKey:#"MailMessage"];
NSManagedObject *newObject = [[NSManagedObject alloc] initWithEntity:entity
insertIntoManagedObjectContext:self.managedObjectContext];
It runs fine when I do not present an MFMailComposeViewController, but if I run this code in the
- (void)mailComposeController:(MFMailComposeViewController*)controller
didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error {
method, it throws the above error when creating the newObject variable.
The entity object when I use print object produces the following:
(<NSEntityDescription: 0x1202e0>) name MailMessage, managedObjectClassName MailMessage,
renamingIdentifier MailMessage, isAbstract 0, superentity name (null), properties {
in both cases, so I don't think the managedObjectContext is completely invalid. I have no idea why it would say MailMessage is not a subclass of NSManagedObject at that point, and not at the other.
Any help would be appreciated, thanks in advance.
Class MailMessage could be implemented in a library or somewhere else in the framework. As Objective C does not implement namespaces, one of the two will be used. Which one is undefined. Try giving your class a different name to quickly resolve the issue.
Look for a message like this from the debugger. It will confirm what Benjamin says.
Class MailMesssage is implemented in both /Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator4.2.sdk/System/Library/PrivateFrameworks/Message.framework/Message and /Users/home/Library/Application Support/iPhone Simulator/4.2/Applications/FFFFFFFF-FFFF-0000-0000-AAAAAAAAAAA/Projects.app/Projects. One of the two will be used. Which one is undefined.
Try resetting the Simulator or uninstalling the application from your device. Often the NSInternalInconsistencyException has to do with problems with changing the datamodel and the database not being updated accordingly.
I was able to workaround this by creating the MailMessage object before presenting the modal view controller. Once the MailMessage object was already created, saving changes did not present a problem. A strange workaround and not addressing the actual problem as far as I know, but it works.