所以我正在尝试使用 Restkit 0.20 映射没有 KVC 的 JSON 对象
没有 KVC 的 JSON
{
"created_at": "2013-03-11T22:13:05Z",
"facebookId": "2343434322",
"firstname": "testuser",
"id": 197,
"mail": "test@gmail.com",
"name": "test",
"phone": "098748394",
"sex": null,
"twitterId": null,
"updated_at": "2013-03-11T22:13:05Z",
"vehiculeDescription": "car",
"recommendCount": 0
}
我的应用委托
RKObjectManager *manager = [RKObjectManager managerWithBaseURL:[NSURL URLWithString:WEBSERVICE]];
// Initialize managed object store
NSURL *modelURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"KojoClient" ofType:@"momd"]];
// NOTE: Due to an iOS 5 bug, the managed object model returned is immutable.
NSManagedObjectModel *managedObjectModel = [[[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL] mutableCopy];
RKManagedObjectStore *managedObjectStore = [[RKManagedObjectStore alloc] initWithManagedObjectModel:managedObjectModel];
manager.managedObjectStore = managedObjectStore;
RKEntityMapping *utilisateurMapping =
[RKEntityMapping mappingForEntityForName:NSStringFromClass([Utilisateur class]) inManagedObjectStore:manager.managedObjectStore];
utilisateurMapping.identificationAttributes = @[@"remoteid"];
[utilisateurMapping addAttributeMappingsFromDictionary:@{
@"remoteid": @"id",
@"name": @"name",
@"firstname": @"firstname",
@"phone": @"phone",
@"facebookid": @"facebookId",
@"mail": @"mail",
@"vehiculeDescription" : @"vehiculeDescription",
@"recommendCount" : @"recommendCount",
@"created_at": @"created_at",
@"updated_at": @"updated_at",
@"twitterId": @"twitterId"
}];
/**
Register our mappings with the provider
*/
RKResponseDescriptor* utilisateursDescriptor =
[RKResponseDescriptor responseDescriptorWithMapping:[utilisateurMapping inverseMapping]
pathPattern:@"/utilisateurs"
keyPath:@""
statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
RKResponseDescriptor* utilisateurDescriptor =
[RKResponseDescriptor responseDescriptorWithMapping:[utilisateurMapping inverseMapping]
pathPattern:@"/utilisateurs/:remoteid"
keyPath:@""
statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
RKResponseDescriptor* facebookUtilisateurDescriptor =
[RKResponseDescriptor responseDescriptorWithMapping:[utilisateurMapping inverseMapping]
pathPattern:@"/face_utilisateurs/:facebookid"
keyPath:@""
statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
[manager addResponseDescriptorsFromArray:@[utilisateursDescriptor, utilisateurDescriptor, facebookUtilisateurDescriptor]];
// Serialize to JSON
manager.requestSerializationMIMEType=RKMIMETypeJSON;
/**
Complete Core Data stack initialization
*/
[managedObjectStore createPersistentStoreCoordinator];
NSString *storePath = [RKApplicationDataDirectory() stringByAppendingPathComponent:@"Application.sqlite"];
NSError *error;
NSPersistentStore *persistentStore = [managedObjectStore addSQLitePersistentStoreAtPath:storePath fromSeedDatabaseAtPath:nil withConfiguration:nil options:@{NSMigratePersistentStoresAutomaticallyOption:@YES, NSInferMappingModelAutomaticallyOption:@YES} error:&error];
NSAssert(persistentStore, @"Failed to add persistent store with error: %@", error);
// Create the managed object contexts
[managedObjectStore createManagedObjectContexts];
// Configure a managed object cache to ensure we do not create duplicate objects
managedObjectStore.managedObjectCache = [[RKInMemoryManagedObjectCache alloc] initWithManagedObjectContext:managedObjectStore.persistentStoreManagedObjectContext];
问题是当我想获得我的对象时:
[[RKObjectManager sharedManager] getObjectsAtPath:path parameters:nil success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
if(![[mappingResult firstObject] isKindOfClass:[Utilisateur class]]) {
//Dont work
}
} nil];
RKMappingResult 对象没有类。
但是映射工作
mappingResult->_keyPathToMappedObjects 的打印描述: { "" = { "created_at" = "2013-03-11T22:13:05Z"; facebookid = 4567898765; 名字 = 测试;邮件=“test@gmail.com”;名称 = 测试名称;电话 = 45678976587;推荐计数 = 0; 远程ID = 197;"updated_at" = "2013-03-11T22:13:05Z"; 车辆描述 = “测试”;}; }
我不明白为什么我的对象映射没有类。
我遵循了没有 KVO 的对象映射教程
并且无法更改服务器 JSON 样式。
更新 1
我尝试不使用 NSStringFromClass,不工作。