我收到核心数据错误,我无法弄清楚如何修复。
我基本上将对象的所有数据都提取到字典中,将数据显示到表单中,并且某些字段允许编辑,然后尝试在提交时将数据存储回对象。
但是,在设置所有新的/更新的值时,我收到错误
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"];
除了这两行外,没有太多的财产使用。它可以被修改,但不能被用户在这个特定的屏幕上修改
答案 0 :(得分:6)
核心数据实体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
的{{1}}对象是NSString