Desired NSNumber type of the given type __NSCFString error

I get a basic data error that I cannot figure out how to fix.

I basically pull all the object data into the dictionary, showing the data in the form, and some fields allow editing, and then try to save the data back to the object when sending.

However, when setting all new / updated values, I get an error

Unacceptable type of value for attribute: property = "totalLocations"; desired type = NSNumber; given type = __NSCFString; value = 7.

Here is the code that processes this 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, the property is not used too much. it can be changed, but not by the user on this particular screen

+5
source share
1 answer

totalLocations Integer myObject.totalLocations NSString? , :

[data setValue:[NSNumber numberWithInteger:[myObject.totalLocations integerValue]] forKey:@"totalLocations"];

:

- (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();
  }
}

id promoJson - NSString

+6

All Articles