Restkit属性映射失败的转换错误 [英] Restkit property mapping Failed Transformation error

查看:148
本文介绍了Restkit属性映射失败的转换错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的示例代码看起来像:

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

c $ c> subject 实体..我的映射工作很好:

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

我得到以下错误:

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

想法?

推荐答案

您不应该有用于考试的回复描述符。

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.

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

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

这篇关于Restkit属性映射失败的转换错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆