问题
this is what my sample code looks like:
{
"name": "Ahmad Mansour",
"subjects": [
{
"parent_subject_name": "Arabic",
"subject_name": "Shafahi",
"exams": [
{
"score": "30.00",
"exam_name": "Sa3i 1 "
},
{
"score": "50.00",
"exam_name": "sa3i 2"
},
{
"score": "100.00",
"exam_name": "First Semester Exam"
}
]
},
{
"parent_subject_name": "Arabic",
"subject_name": "Khati",
"exams": [
{
"score": "50.00",
"exam_name": "Sa3i 1 "
},
{
"score": "60.00",
"exam_name": "sa3i 2"
},
{
"score": "95.00",
"exam_name": "First Semester Exam"
}
]
},
for the subject
entity.. my mapping works just fine:
RKEntityMapping *subjectEntityMapping = [RKEntityMapping mappingForEntityForName:kSubjectIdentity inManagedObjectStore:model.managedObjectStore];
[subjectEntityMapping addAttributeMappingsFromDictionary:@{ @"subject_name": @"name",
@"parent_subject_name" :@"parent_name"
}];
subjectEntityMapping.identificationAttributes = @[ @"name" ];
RKResponseDescriptor *studentResponseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:subjectEntityMapping pathPattern:kGradesPath keyPath:@"subjects" statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
[model.objectManager addResponseDescriptor:studentResponseDescriptor];
but when i do the exam score mapping.. things blow up:
RKEntityMapping *examEntityMapping = [RKEntityMapping mappingForEntityForName:kExamIdentity inManagedObjectStore:model.managedObjectStore];
[examEntityMapping addAttributeMappingsFromDictionary:@{ @"exams.exam_name": @"name" }];
examEntityMapping.identificationAttributes = @[ @"name" ];
RKResponseDescriptor *examResponseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:examEntityMapping pathPattern:kGradesPath keyPath:@"subjects" statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
[model.objectManager addResponseDescriptor:examResponseDescriptor];
i get the following error:
E restkit.object_mapping:RKMappingOperation.m:431 Failed transformation of value at keyPath 'exams.exam_name' to representation of type 'NSString': Error Domain=org.restkit.RKValueTransformers.ErrorDomain Code=3002 "Failed transformation of value '(
"Sa3i 1 ",
"sa3i 2",
"First Semester Exam"
)' to NSString: none of the 2 value transformers consulted were successful." UserInfo=0x9a508a0 {detailedErrors=(
"Error Domain=org.restkit.RKValueTransformers.ErrorDomain Code=3002 \"The given value is not already an instance of 'NSString'\" UserInfo=0x9a50800 {NSLocalizedDescription=The given value is not already an instance of 'NSString'}",
"Error Domain=org.restkit.RKValueTransformers.ErrorDomain Code=3000 \"Expected an `inputValue` of type `NSNull`, but got a `__NSArrayI`.\" UserInfo=0x9a50830 {NSLocalizedDescription=Expected an `inputValue` of type `NSNull`, but got a `__NSArrayI`.}"
I also tried this mapping, but i get the exact same error:
[examEntityMapping addAttributeMappingsFromDictionary:@{ @"exam_name": @"name" }];
examEntityMapping.identificationAttributes = @[ @"name" ];
RKResponseDescriptor *examResponseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:examEntityMapping pathPattern:kGradesPath keyPath:@"subjects.exams" statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
ideas?
回答1:
You shouldn't have a response descriptor for exams. It's nested data in your subject so it should be mapped using a relationship on the subject mapping.
Using the response descriptor doesn't work because you're trying to map into 2 arrays when the mapping only caters for one. Hence you get an error when RestKit tries to convert an array into a string.
Also, for your exam mapping you should probably specify multiple attributes for the unique identity as exam names are used repeatedly for different instances...
来源:https://stackoverflow.com/questions/19607672/restkit-property-mapping-failed-transformation-error