我试图在RKEntityMapping中使用RestKit;( 救命啊!
NSURL *baseURL = [NSURL URLWithString:@"http://api.mymemory.translated.net"];
RKObjectManager *objectManager = [RKObjectManager managerWithBaseURL:baseURL];
// Initialize managed object model from bundle
NSManagedObjectModel *managedObjectModel = [NSManagedObjectModel mergedModelFromBundles:nil];
// Initialize managed object store
RKManagedObjectStore *managedObjectStore = [[RKManagedObjectStore alloc] initWithManagedObjectModel:managedObjectModel];
objectManager.managedObjectStore = managedObjectStore;
// Complete Core Data stack initialization
[managedObjectStore createPersistentStoreCoordinator];
NSString *storePath = [RKApplicationDataDirectory() stringByAppendingPathComponent:@"MyArticlesDB.sqlite"];
NSString *seedPath = [[NSBundle mainBundle] pathForResource:@"RKSeedDatabase" ofType:@"sqlite"];
NSError *error;
NSPersistentStore *persistentStore = [managedObjectStore addSQLitePersistentStoreAtPath:storePath fromSeedDatabaseAtPath:seedPath withConfiguration:nil options:nil 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];
RKEntityMapping *matchMapping = [RKEntityMapping mappingForEntityForName:@"Match" inManagedObjectStore:managedObjectStore];
[matchMapping addAttributeMappingsFromDictionary:@{@"id" :@"id",
@"segment" :@"segment",
@"trnaslation" :@"translation",
@"quality" :@"quality",
@"reference" :@"reference",
@"usage-count" :@"usage_count",
@"subject" :@"subject",
@"created-by" :@"MateCat",
@"last-updated-by" :@"last_updated_by",
@"create-date" :@"create_date",
@"last-update-date" :@"last_update_date",
@"tm_properties" :@"tm_properties",
@"match" :@"match"}];
RKEntityMapping *translationMapping = [RKEntityMapping mappingForEntityForName:@"Translation" inManagedObjectStore:managedObjectStore];
[translationMapping addAttributeMappingsFromDictionary:@{@"responseData.translatedText" :@"translatedText",
@"responseData.match" :@"match",
@"responseDetails" :@"responseDetails",
@"responseStatus" :@"responseStatus",
@"responseId" :@"responseId"}];
RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:translationMapping method:RKRequestMethodGET pathPattern:@"/" keyPath:nil statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
[objectManager addResponseDescriptor:responseDescriptor];
json:http://api.mymemory.translated.net/get?q=Hello%20World!&langpair=en%7Cru
错误:
错误Domain = org.restkit.RestKit.ErrorDomain Code = 1001"没有响应描述符与加载的响应匹配。" UserInfo = {NSLocalizedFailureReason =从URL' http://api.mymemory.translated.net/get?q=Hello%20World!&langpair=en%7Cru'加载了200响应,无法匹配所有(0)响应描述符:,NSErrorFailingURLStringKey = http://api.mymemory.translated.net/get?q=Hello%20World!&langpair=en%7Cru,NSErrorFailingURLKey = { {3}},NSUnderlyingError = 0x1700562f0 {Error Domain = org.restkit.RestKit.ErrorDomain Code = 1001"在搜索的关键路径中找不到可映射的对象表示。" UserInfo = {NSLocalizedDescription =在搜索的关键路径中找不到可映射的对象表示。,NSLocalizedFailureReason =映射操作无法在搜索的关键路径中找到任何嵌套对象表示: 发现输入到映射器的表示包含以下键路径的嵌套对象表示:matches,responderId,responseData,responseDetails,responseStatus 这可能表示您错误配置了映射的关键路径。,keyPath = null,DetailedErrors =( )},keyPath = null,NSLocalizedDescription =没有响应描述符匹配加载的响应。}
答案 0 :(得分:0)
我忘了添加关系。这帮助了我。
RKRelationshipMapping *res = [RKRelationshipMapping relationshipMappingFromKeyPath:@"responseData" toKeyPath:@"responseData" withMapping:responseMapping];
RKRelationshipMapping *matchRelation = [RKRelationshipMapping relationshipMappingFromKeyPath:@"matches" toKeyPath:@"matches" withMapping:matchMapping];
[globalMappig addPropertyMapping:matchRelation];
[globalMappig addPropertyMapping:responseRelation];