Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

desired type NSNumber given type __NSCFString error

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

like image 979
JMD Avatar asked Apr 18 '13 15:04

JMD


1 Answers

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

like image 121
Jeremy Avatar answered Sep 28 '22 01:09

Jeremy