Restkit 属性映射失败的转换错误

Posted

技术标签:

【中文标题】Restkit 属性映射失败的转换错误【英文标题】:Restkit property mapping Failed Transformation error 【发布时间】:2013-10-26 14:08:08 【问题描述】:

这是我的示例代码的样子:


    "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"
                
            ]
        ,

对于subject 实体.. 我的映射工作正常:

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];

但是当我做考试分数映射时..事情爆炸了:

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];

我收到以下错误:

 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`."

我也试过这个映射,但我得到了完全相同的错误:

[examEntityMapping addAttributeMappingsFromDictionary:@ @"exam_name": @"name" ];

examEntityMapping.identificationAttributes = @[ @"name" ];

RKResponseDescriptor *examResponseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:examEntityMapping pathPattern:kGradesPath keyPath:@"subjects.exams" statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];

想法?

【问题讨论】:

【参考方案1】:

您不应该有考试的响应描述符。它是您的主题中的嵌套数据,因此应使用主题映射上的关系对其进行映射。

使用响应描述符不起作用,因为当映射只满足一个时,您试图映射到 2 个数组。因此,当 RestKit 尝试将数组转换为字符串时会出现错误。

此外,对于您的考试映射,您可能应该为唯一身份指定多个属性,因为考试名称会重复用于不同的实例...

【讨论】:

我不确定如何处理您在上面提供的信息.. 一方面您说这应该是一个主题映射.. 另一方面您说的是主题与否,我将 2 个数组映射为一个。你能告诉我一些应该解决问题的代码吗?是主题映射还是考试映射? 您需要科目和考试映射。但是您只需要 1 个响应描述符 - 主题之一。然后,您应该在 2 个映射(和实体)之间建立关系。 github.com/RestKit/RestKit/wiki/Object-mapping#relationships

以上是关于Restkit 属性映射失败的转换错误的主要内容,如果未能解决你的问题,请参考以下文章

RestKit CoreData 0.20.3 - 映射完成后保存 MOC 之前的额外检查

RestKit - 实体映射属性转换

RestKit 没有从 JSON 映射图像

RestKit 对象映射 POST 响应,“CoreData:错误:在 NSManagedObject 类映射操作上调用指定初始化程序失败”

Restkit给出映射错误:

RestKit & Cocoapods:映射操作失败/没有找到任何可映射的值