我正在使用 RestKit 将嵌套的核心数据模型转换为 JSON 文件并将其上传到 Web 服务。然后我试图获取相同的 JSON 请求并将其重新膨胀到核心数据对象中。我在通货膨胀期间收到此错误:
CoreData: error: Failed to call designated initializer on NSManagedObject class 'AppUser'
2012-04-26 10:25:50.850 DropboxSync[3824:5843] *** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<AppUser 0x2b7820> valueForUndefinedKey:]: the entity (null) is not key value coding-compliant for the key "lastName".'
该类有一个 lastName 属性:
@property (nonatomic, retain) NSString * firstName;
@property (nonatomic, retain) NSString * lastName;
@property (nonatomic, retain) NSString * localDataFilepath;
@property (nonatomic, retain) NSSet *events;
@property (nonatomic, retain) AppUserWrapper *wrapper;
**What could be causing this?**
我在我的核心数据模型中定义了 3 个实体:
用户事件图像
用户有多个事件,事件有多个图像
我已经定义了一个如下定义的 RestKit 对象映射。
-(void)setupObjectMapping { RKObjectManager *objectManager = [RKObjectManager sharedManager ] ; RKObjectMapping *mapping = [RKObjectMapping mappingForClass:[文章类]];
[mapping mapAttributes:@"articleID", @"title", @"body", nil];
//********************************
RKObjectMapping *imageMapping = [RKObjectMapping mappingForClass:[ImageEntity class]];
[imageMapping mapAttributes:@"createDate", @"localFilePath", nil];
[objectManager.mappingProvider addObjectMapping:bleedImageMapping];
[objectManager.mappingProvider setSerializationMapping:[imageMapping inverseMapping] forClass:[ImageEntity class]];
[objectManager.mappingProvider imageMapping forKeyPath:@"images"];
//********************************
RKObjectMapping *eventMapping = [RKObjectMapping mappingForClass:[Event class]];
[eventMapping mapAttributes:@"createDate", @"severity", nil];
[eventMapping mapRelationship:@"images" withMapping:imageMapping];
[objectManager.mappingProvider addObjectMapping:eventMapping];
[objectManager.mappingProvider setSerializationMapping:[eventMapping inverseMapping] forClass:[Event class]];
[objectManager.mappingProvider setMapping:eventMapping forKeyPath:@"bleedEvents"];
//********************************
//setup App user mapping
RKObjectMapping *userMapping = [RKObjectMapping mappingForClass:[AppUser class]];
[userMapping mapAttributes:@"firstName", @"lastName", nil];
[userMapping mapRelationship:@"events" withMapping:eventMapping];
[objectManager.mappingProvider addObjectMapping:userMapping];
[objectManager.mappingProvider setSerializationMapping:[userMapping inverseMapping] forClass:[AppUser class]];
[objectManager.mappingProvider setMapping:userMapping forKeyPath:@"appUser"];
//********************************
//setup App user wrapper mapping
RKObjectMapping *userWrapperMapping = [RKObjectMapping mappingForClass:[AppUserWrapper class]];
[userWrapperMapping mapRelationship:@"appUser" withMapping:userMapping];
[objectManager.mappingProvider addObjectMapping:userWrapperMapping];
[objectManager.mappingProvider setSerializationMapping:[userWrapperMapping inverseMapping] forClass:[AppUserWrapper class]];
[objectManager.mappingProvider setMapping:userWrapperMapping forKeyPath:@"appUserWrapper"];
}
以下是我将嵌套数据结构转换为 JSON 的方法:
-(void)convertTestUserToJSON
{
NSString* fullPath = [[$ documentPath] stringByAppendingPathComponent:@"sampleJSONFolder"];
[[NSFileManager defaultManager] createDirectoryAtPath:fullPath withIntermediateDirectories:YES attributes:nil error:nil];
// Make the file
NSString* fullFilepath = [NSString stringWithFormat:@"%@_%@.json",appUser.firstName,appUser.lastName];
NSString* dataFile = [fullPath stringByAppendingPathComponent:fullFilepath];
appUser.localDataFilepath = dataFile;
[[AppUser managedObjectContext] save:nil];
NSError* error = nil;
RKObjectMapping *serMap = [[[RKObjectManager sharedManager] mappingProvider] serializationMappingForClass:[AppUser class]];
NSDictionary *d = [[RKObjectSerializer serializerWithObject:appUser mapping:serMap] serializedObject:&error];
if(error!=nil)
{
NSLog(@"!!!!! Error: %@",[error localizedDescription]);
}
//this is where the JSON is generated
NSString* dataContents = [d JSONString];
BOOL success = [dataContents writeToFile:dataFile atomically:YES encoding:NSUTF8StringEncoding error:nil];
if(!success)
{
NSLog(@"Error writing to data file!");
}
}
这是JSON转换的结果
{"firstName":"First Name0","events":[{"severity":0,"images":[{"createDate":"2005-04-21 08:28:47 +0000","localFilePath":"localPhoto#10.png"},{"createDate":"2009-12-19 07:26:54 +0000","localFilePath":"localPhoto#11.png"}],"createDate":"2003-05-25 15:32:53 +0000"},{"severity":1,"images":[{"createDate":"2008-08-02 19:40:14 +0000","localFilePath":"localPhoto#10.png"},{"createDate":"2007-01-12 05:50:27 +0000","localFilePath":"localPhoto#12.png"},{"createDate":"2007-05-28 12:19:39 +0000","localFilePath":"localPhoto#11.png"}],"createDate":"2001-05-28 23:38:23 +0000"},{"severity":2,"images":[{"createDate":"2002-10-09 19:47:39 +0000","localFilePath":"localPhoto#10.png"},{"createDate":"2008-08-17 03:21:12 +0000","localFilePath":"localPhoto#11.png"}],"createDate":"2005-10-18 03:40:52 +0000"}],"lastName":"Last Name83"}
当我尝试将此 JSON 膨胀回嵌套的核心数据实体时,出现错误:
CoreData: error: Failed to call designated initializer on NSManagedObject class 'AppUser'
2012-04-26 10:25:50.850 DropboxSync[3824:5843] *** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<AppUser 0x2b7820> valueForUndefinedKey:]: the entity (null) is not key value coding-compliant for the key "lastName".'
在尝试映射之前,inflater 似乎没有实例化所有关系。我错了吗?什么可能导致这种情况发生?我试图以两种不同的方式重新膨胀我的 JSON,但两次我都得到了这个错误。如何解决?
感谢您的任何意见!