RestKit Map values ​​from a nested dictionary

I get a JSON response like this

{ "id" : 12345 "course_name" : "history", "teacher" : "joy", "region" : { "code" : "Al", "name" : "Alabama" } } 

I have a course object in coredata and a corresponding model in code like "MKCourse", this object is similar to this

 MKCourse - id - courseName - teacher - regionCode - regionName 

I am setting values ​​from a nested dictionary in MKCourse, like this:

 mapping = [RKManagedObjectMapping mappingForClass:[self class] inManagedObjectStore:[[RKObjectManager sharedManager] objectStore]]; mapping.setDefaultValueForMissingAttributes = YES; mapping.setNilForMissingRelationships = YES; [mapping mapKeyPathsToAttributes: @"id", [self modelIdAttribute], @"course_name", @"courseName", @"teacher", @"teacher", @"region.code", @"regionCode", @"region.name", @"regionName", nil]; 

But it always sets nil to regionCode and regionName. I do not know what is wrong. Is it possible to get such values.

+4
source share
1 answer

for RestKit 2. + add the following code:

 [mapping addAttributeMappingFromKeyOfRepresentationToAttribute:@"region"]; 

and try the addAttributeMappingsFromDictionary method

 [mapping addAttributeMappingsFromDictionary:@{ @"id", [self modelIdAttribute], @"course_name", @"courseName", @"teacher", @"teacher", @"region.code", @"regionCode", @"region.name", @"regionName" }]; 

not sure about RestKit 1.0. maybe you can try to separate them:

 [mapping mapKeyPath:@"id" toAttribute:[self modelIdAttribute]]; [mapping mapKeyPath:@"course_name" toAttribute:@"courseName"]; [mapping mapKeyPath:@"teacher" toAttribute:@"teacher"]; [mapping mapKeyPath:@"region.code" toAttribute:@"regionCode"]; [mapping mapKeyPath:@"region.name" toAttribute:@"regionName"]; 
+2
source

Source: https://habr.com/ru/post/1479331/


All Articles