Problem keeping NSDate in CoreData context

I am trying to save a managed object with one of the attributes of type Date in managedObjectContext .

The code is similar:

 reminder.eventDate = selectedDate; NSLog(@"Date: %@", selectedDate); NSError *error = nil; if (![reminder.managedObjectContext save:&error]) { NSLog(@"Unresolved error %@, %@", error, [error userInfo]); abort(); } 

during an emergency stop of a program with SIGABRT. Here is the console log:

 2011-02-28 00: 50: 18.817 MyApp [9021: 207] *** Terminating app due to uncaught 
 exception 'NSInvalidArgumentException', reason: '- [__ NSDate isEqualToString:]: 
 unrecognized selector sent to instance 0x4e73490 '
 *** Call stack at first throw:
 (
     0 CoreFoundation 0x01057be9 __exceptionPreprocess + 185
     1 libobjc.A.dylib 0x011ac5c2 objc_exception_throw + 47
     2 CoreFoundation 0x010596fb - [NSObject (NSObject) doesNotRecognizeSelector:] + 187
     3 CoreFoundation 0x00fc9366 ___forwarding___ + 966
     4 CoreFoundation 0x00fc8f22 _CF_forwarding_prep_0 + 50

Does anyone know why I have this? The second question is why, when I check the debug mode of selectedDate , it is not an NSDate type, but __NSDate (double underscore in front).

Thanks!

UPDATE:

I made some changes to make it easier to catch a mistake. so the code is now similar:

 reminder.eventDate = [NSDate date]; NSLog(@"Date: %@", selectedDate); NSError *error = nil; if (![reminder.managedObjectContext save:&error]) { NSLog(@"Unresolved error %@, %@", error, [error userInfo]); abort(); } 

So here we are exactly saving the NSDate. reminder.eventDate is also an NSDate. But I still have this error. If I comment on reminder.eventDate = [NSDate date]; save, then another error (date is a required field in NSData, so save: returns the error "Operation could not be completed") using eventDate = nil; . The CoreData structure is checked several times - eventDate has a date type.

UPDATE (problem resolved): I finally found the problem. eventDate was set as the key for detailTextLabel.text in my tableview cells (I used KVO for this). So if there wasn’t a direct call, and I couldn’t find any invokation method for eventDate . The strange thing is that the accident was by the save method, and not later. And in the call stack there is no tableView:cellForRowAtIndexPath: method tableView:cellForRowAtIndexPath: in general ...

+8
ios objective-c iphone core-data
source share
5 answers

Just to add to the above:

This should not be a problem with the model or subclass of NSManagedObject. It could just be a piece of code almost anywhere, calling isEqualToString: Somewhere you have an object that assumes code should be NSString, but instead is an NSDate object. I would look at any code that could convert dates to strings.

However, since this happens when saving, I would look at any settings that you could make to the subclass.

A place to start is to search for the isEqualToString: project.

Update:

Since isEqualToString is a testing method, it can be activated at any time when you are comparing a string, for example, in sorting. I could not reproduce your exact error, but the following code does something like this:

 id idDate=[NSDate date]; NSString *bob=[NSString stringWithString:@"bob"]; NSString *steve=@"test this"; steve=idDate; NSLog(@"test=%@",([steve compare:bob]) ?@"YES":@"NO"); 

... throws an exception:

 -[NSCFString timeIntervalSinceReferenceDate]: unrecognized selector sent to instance 0x4040 

So you can see how these types of errors can occur. This is also a demonstration of why you should be careful using id .

+2
source share

It's silly how that sounds, I would suggest you double check that both reminder objects and the associated ObjectContext managed object are valid.

You are sure that your model correctly models eventDate as a date attribute, and you are also sure that the object you pass to the eventDate attribute is an NSDate object. So, try checking that the reminder and its managedObjectContext are not zero.

Finally, check that all other attributes and relationships are set correctly (check if you really insert all the necessary attributes and relationships, check to see if the objects you are using have the correct type, etc.).

It will also help narrow down opportunities.

EDIT . You can check what is actually happening in your sqlite database. Before adding an application, simply add the following as an argument:

-com.apple.CoreData.SQLDebug 1

see documentation .

How you do this depends on your installed toolbox (Xcode 3 or 4). I use the latest Xcode 4 and add the argument that you just select for the current scheme, then select the "Run" name of your application and go to the "Arguments" tab to add the argument.

+7
source share

It looks like your data model is set to wait for a string in the eventDate property, not a date.

+6
source share

NSDate is a class cluster, which means that the instances you are really dealing with will be instances of some private subclass of NSDate (in this case __NSDate).

Regarding the error, I agree with Simon Golden that the most likely reason is that the data model or subclass of NSManagedObject expects a string rather than a date.

+3
source share

I also chased this one. Since your definitions of models and classes are good, and if you think your value is good, then there is one less obvious place that fixed this for me ... Uninstall the application from the device and clean your assembly and make sure that all sqlite files that are seeded are new too. In my folder with the device or assembly there was an old drive or model that caused this. What other framework do you use? RestKit, JSON, etc.? Everything that overloads a class method can also be suspect .... it's hard to say what compares in this save ... My guess is the old store

0
source share

All Articles