所以我尝试使用Restkit 0.20
映射没有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; firstname =测试; mail =“test@gmail.com”; name = TestName; 电话= 45678976587; recommendedCount = 0; remoteid = 197; “updated_at”=“2013-03-11T22:13:05Z”; vehiculeDescription =“test”; }; }
我不明白为什么我的对象映射没有类。
我已关注Object Mapping Tutorial without KVO
并且无法更改服务器JSON样式。
我试着没有NSStringFromClass,不行。
答案 0 :(得分:0)
为什么[utilisateurMapping inverseMapping]
使用RKResponseDescriptor
RKEntityMapping
?也许如果你尝试:
RKResponseDescriptor* utilisateursDescriptor =
[RKResponseDescriptor responseDescriptorWithMapping:utilisateurMapping
pathPattern:@"/utilisateurs"
keyPath:@""
statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];