2013-08-01 319 views
2

我試圖映射來自所述服務器具有2個屬性的響應:對象映射Restkit不映射嵌套對象

  • 那就是OBJETS陣列
  • 一個在計數的Number數組用戶objets,我已經在其他類中映射。

這是我怎麼有類模型:

enter image description here

及其相關與關係「參與者」的用戶類別。

我知道,我要地圖是工作,因爲我在其他類中使用這個偉大工程的嵌套類,但這種實現是不同的,所以我不知道我怎麼可以映射此。

我有這個在我的課:

.H類:

#import <Foundation/Foundation.h> 
#import <CoreData/CoreData.h> 
#import "NSManagedObject+Convenience.h" 

@interface DUParticipant : NSManagedObject 

@property (nonatomic, retain) NSNumber * participantsCount; 
@property (nonatomic, retain) NSSet *participants; 

+ (RKEntityMapping *)mapping; 
+ (void)configMapping; 

@end 

.M類

#import "DUParticipant.h" 
#import "DUUser+Ext.h" 

@implementation DUParticipant 

@dynamic participantsCount; 
@dynamic participants; 

+ (RKEntityMapping *)mapping 
{ 
    RKEntityMapping* map = [super mapping]; 

    [map addAttributeMappingsFromDictionary:@{ 
     @"participants_count" : @"participantsCount" 
    }]; 


    return map; 
} 

+(void) configMapping{ 


    RKObjectManager* man= [RKObjectManager sharedManager]; 
    RKEntityMapping* map = [self mapping]; 
    [man addResponseDescriptor:[RKResponseDescriptor responseDescriptorWithMapping:map pathPattern:@"/v1/users/participants" keyPath:nil 
                     statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)]]; 

    RKObjectMapping *requestMapping = [RKObjectMapping requestMapping]; 
    [requestMapping addAttributeMappingsFromDictionary:@{ 
     @"participants_count" : @"participantsCount" 
    }]; 

    [map addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"participants" 
                     toKeyPath:@"participants" 
                     withMapping:[DUUser mapping]]]; 

    [man addRequestDescriptor:[RKRequestDescriptor requestDescriptorWithMapping:requestMapping 
                    objectClass:[DUParticipant class] rootKeyPath:@"DUParticipant"]]; 

} 

@end 

並且json的響應是:

{ 
    "participants_count": 1, 
    "participants": [ 
     { 
      "participant": { 
       "id": 3, 
       "gender": "M", 
       "name": "Oscar", 
       "lastname": "Lares", 
       "username": "CaLe", 
       "email": "[email protected]", 
       "bio": "UX Designer! Mac Geek, Computers, Gadgets and Movies lover!", 
       "created_at": "2012-07-28T18:34:27Z", 
       "updated_at": "2013-07-29T13:05:21Z", 
       "cover": null, 
       "avatar": { 
        "id": 108, 
        "original": "https://duuin.s3.amazonaws.com/uploads/image/file/108/photo.jpg", 
        "thumbnail": "https://duuin.s3.amazonaws.com/uploads/image/file/108/thumb_photo.jpg" 
       } 
      } 
     } 
    ] 

**** **編輯** **** 這是我在日誌主機,它的JSON響應:

response.body = 
{ 
    "participants_count": 2, 
    "participants": [ 
     { 
      "participant": { 
       "id": 3, 
       "gender": "M", 
       "name": "blabla", 
       "lastname": "blabla", 
       "username": "blabla", 
       "email": "blabla", 
       "bio": "blablablablablabla", 
       "created_at": "2012-07-28T18:34:27Z", 
       "updated_at": "2013-07-29T13:05:21Z", 
       "cover": null, 
       "avatar": { 
        "id": 108, 
        "original": "blabla", 
        "thumbnail": "blabla" 
       } 
      } 
     }, 
     { 
      "participant": { 
       "id": 1, 
       "gender": "M", 
       "name": "blabla", 
       "lastname": "blabla", 
       "username": "blabla", 
       "email": "blabla", 
       "bio": "blabla", 
       "created_at": "2012-07-22T20:30:18Z", 
       "updated_at": "2013-07-31T03:52:26Z", 
       "cover": { 
        "id": 21, 
        "original": "blabla", 
        "thumbnail": "blabla" 
       }, 
       "avatar": { 
        "id": 19, 
        "original": "blabla", 
        "thumbnail": "blabla" 
       } 
      } 
     } 
    ] 
} 

並在日誌中的對象:

2013-08-01 13:35:43.949 duuin[24550:c07] Participant: <NSManagedObject: 0x16d7ef00> (entity: DUParticipant; id: 0x16d74170 <x-coredata://1C844BC7-AF17-4D27-90F9-9F5B8ACDF848/DUParticipant/p86> ; data: { 
    participants = nil; 
    participantsCount = 2; 
}) 
+0

結果是什麼,並有你開啓跟蹤記錄看發生了什麼? – Wain

+0

@Wain看到編輯的問題,兄弟 – FxckDead

回答

1

我認爲問題出在你的嵌套類映射上。它可能在其他時間工作,但我認爲背景是不同的。在所示代碼中的參與者的字典裏面participant鍵內。這需要反映在映射中的所有關鍵字中。


想必你的映射目前有:

@"gender" -> @"gender" 

但對於JSON告訴你它需要:

@"participant.gender" -> @"gender" 
+0

我不明白你的意思是什麼「這需要在所有的映射中的鍵得以體現。」 @Wain – FxckDead