5

我遇到了一个核心数据错误,我不知道如何修复。

我基本上是将对象的所有数据提取到字典中,将数据显示到表单中,并且某些字段允许编辑,然后尝试在提交时将数据存储回对象。

但是,在设置所有新/更新的值时,我得到了错误

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

这是处理此特定属性的代码...

    //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"];

除了这两行之外,该属性没有太多使用。它可以修改,但不能由用户在此特定屏幕上修改

4

1 回答 1

7

totalLocations您的核心数据实体中的类型IntegermyObject.totalLocationsNSString 吗?如果是,您应该像这样设置核心数据:

[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

于 2013-04-18T15:13:59.347 回答