I'm getting a core data error that I can't figure out how to fix.
I am basically pulling out all the data of an object into a dictionary, showing the data to a form, and some fields allow editing, then trying to store the data back to the object on submission.
However, on setting all the new/updated values I get the error
Unacceptable type of value for attribute: property = "totalLocations"; desired type = NSNumber; given type = __NSCFString; value = 7.
Here is the code that handles this particular property...
//grab the value from the property
if (myObject.totalLocations)
[data setObject:myObject.totalLocations forKey:@"totalLocations"];
// store it back to the object
_myObject.totalLocations = [data objectForKey:@"totalLocations"];
aside from these two lines there isn't too much usage of the property. it can be modified, but not by the user on this particular screen
Is the type of totalLocations
in your core data entity Integer
and is myObject.totalLocations
a NSString? If yes you should set the core data like this:
[data setValue:[NSNumber numberWithInteger:[myObject.totalLocations integerValue]] forKey:@"totalLocations"];
The way I set my managed objects is like this:
- (void)insertNewPromo:(NSDictionary *)promoJson
{
NSManagedObjectContext *context = [self.promoFetchedResultsController managedObjectContext];
NSEntityDescription *entity = [[self.promoFetchedResultsController fetchRequest] entity];
NSManagedObject *newManagedObject = [NSEntityDescription insertNewObjectForEntityForName:[entity name] inManagedObjectContext:context];
// Checking if inappropriate data are in the JSON to avoid some crashes.
if ([[promoJson objectForKey:@"id"] isKindOfClass:[NSNull class]])
[newManagedObject setValue:nil forKey:@"id"];
else
[newManagedObject setValue:[NSNumber numberWithInteger:[[promoJson objectForKey:@"id"] integerValue]] forKey:@"id"];
...
...
NSError *error = nil;
if (![context save:&error])
{
if (DEBUG_ON == 1)
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
}
the id
object of promoJson
is a NSString
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With