2013-10-26 28 views
0

這是我的示例代碼是什麼樣子:Restkit屬性映射失敗的轉型錯誤

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

想法?

回答

2

您不應該有考試的回覆描述符。它嵌套在主題中的數據,所以它應該使用主題映射關係進行映射。

使用響應描述符不起作用,因爲當映射僅適用於一個映射時,您試圖映射到2個數組。因此,當RestKit嘗試將數組轉換爲字符串時會出現錯誤。

此外,對於考試的映射,你應該可能是因爲考試的名字被反覆用於不同的情況下,唯一的身份指定多個屬性...

+0

我不知道做什麼用你給的信息上面..一方面你說這應該是一個主題映射..在另一個你說的主題或不,我映射2個數組在一個.. 你可以請給我看一些代碼應該解決問題?無論是主題映射還是考試映射? – abbood

+0

你需要主題和考試映射。但你只需要1個響應描述符 - 主題。那麼你應該有2個映射(和實體)之間的關係。 https://github.com/RestKit/RestKit/wiki/Object-mapping#relationships – Wain