2014-01-27 298 views
0

我有這個問題,我正確映射所有對象,但在根對象內的屬性,出於某種原因,我無法映射。RestKit映射對象沒有對象類

這是JSON:

{ 
    "responseError": null, 
    "responseIsRegisteredServices": { 
    "registeredToServiceAntiVirus": true, 
    "registeredToServiceCloud": true, 
    "registeredToServiceMusic": true, 
    "registeredToServiceSong": true, 
    "registeredToServiceTv": true 
    }, 
    "responseRegisteredServicesCloud": null, 
    "responseRegisteredServicesMusic": [ 
    { 
     "ID": null, 
     "Name": null, 
     "AlbumPic": "110_110.png", 
     "ArtistName": "Rihanna" 
    }, 
    { 
     "ID": null, 
     "Name": null, 
     "AlbumPic": "110_110.png", 
     "ArtistName": "Rihanna" 
    }, 
    { 
     "ID": null, 
     "Name": null, 
     "AlbumPic": "110_110.png", 
     "ArtistName": "Steve Angello" 
    } 
    ] 
} 

這是RegisteredServices類:

@interface RegisteredServices : NSObject 
@property (nonatomic) ResponseError *responseError; 
@property (nonatomic) NSNumber *registeredToServiceAntiVirus; 
@property (nonatomic) NSNumber *registeredToServiceCloud; 
@property (nonatomic) NSNumber *registeredToServiceMusic; 
@property (nonatomic) NSNumber *registeredToServiceSong; 
@property (nonatomic) NSNumber *registeredToServiceTv; 
@property (nonatomic) CloudServices *responseRegisteredServicesCloud; 
@property (nonatomic) NSArray *responseRegisteredServicesMusix; 
@end 

這是 'CloudServices' 類:

@interface CloudServices : NSObject 
@property (nonatomic) NSString *capacityType; 
@property (nonatomic) NSNumber *currentCapacity; 
@property (nonatomic) NSString *lastBackupDate; 
@property (nonatomic) NSNumber *percentage; 
@property (nonatomic) NSNumber *totalCapacity; 
@end 

這是 'MusicServices'等級:

@interface MusicServices : NSObject 
@property (nonatomic) NSNumber *idNum; 
@property (nonatomic) NSString *name; 
@property (nonatomic) NSString *albumPic; 
@property (nonatomic) NSString *artistName; 
@end 

這是映射本身:

RKObjectMapping *musixMapping = [RKObjectMapping mappingForClass:[MusicServices class]]; 

[musicMapping addAttributeMappingsFromDictionary:@{ @"ID" : @"idNum", 
                @"Name" : @"name", 
                @"AlbumPic" : @"albumPic", 
                @"ArtistName" : @"artistName" }]; 

RKObjectMapping *cloudMapping = [RKObjectMapping mappingForClass:[CloudServices class]]; 

[cloudMapping addAttributeMappingsFromDictionary:@{@"capacityType": @"capacityType", 
                @"currentCapacity": @"currentCapacity", 
                @"lastBackupDate": @"lastBackupDate", 
                @"percentage": @"percentage", 
                @"totalCapacity": @"totalCapacity" }]; 

RKObjectMapping *registeredServicesMapping = [RKObjectMapping mappingForClass:[RegisteredServices class]]; 

[registeredServicesMapping addAttributeMappingsFromDictionary:@{ @"registeredToServiceAntiVirus" : @"registeredToServiceAntiVirus", 
                   @"registeredToServiceCloud" : @"registeredToServiceCloud", 
                   @"registeredToServiceMusic" : @"registeredToServiceMusic", 
                   @"registeredToServiceSong" : @"registeredToServiceSong", 
                   @"registeredToServiceTv" : @"registeredToServiceTv", }]; 

[registeredServicesMapping addRelationshipMappingWithSourceKeyPath:@"responseRegisteredServicesMusic" mapping:musixMapping]; 

[registeredServicesMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"responseError" 
                          toKeyPath:@"responseError" 
                         withMapping:errorMapping]]; 

[registeredServicesMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"responseRegisteredServicesCloud" 
                          toKeyPath:@"responseRegisteredServicesCloud" 
                         withMapping:cloudMapping]]; 

[[RKObjectManager sharedManager] addResponseDescriptor:[RKResponseDescriptor responseDescriptorWithMapping:registeredServicesMapping 
                            method:RKRequestMethodGET 
                           pathPattern:@"registeredServices" 
                            keyPath:nil 
                           statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)]]; 

想不通什麼似乎是這個問題。當我將responseIsRegisteredServices添加到RKResponseDescriptorkeyPath:中時,MusicServices數組爲空。

在此先感謝。

回答

0

所以,我找到了答案,在這條線:

[registeredServicesMapping addAttributeMappingsFromDictionary:@{ @"registeredToServiceAntiVirus" : @"registeredToServiceAntiVirus", 
                   @"registeredToServiceCloud" : @"registeredToServiceCloud", 
                   @"registeredToServiceMusic" : @"registeredToServiceMusic", 
                   @"registeredToServiceSong" : @"registeredToServiceSong", 
                   @"registeredToServiceTv" : @"registeredToServiceTv", }]; 

我需要把它從registeredToServiceAntiVirus更改爲responseIsRegisteredServices .registeredToServiceAntiVirus,比它的工作完美。這是它的外觀:

[registeredServicesMapping addAttributeMappingsFromDictionary:@{ @"responseIsRegisteredServices.registeredToServiceAntiVirus" : @"registeredToServiceAntiVirus", 
                   @"responseIsRegisteredServices.registeredToServiceCloud" : @"registeredToServiceCloud", 
                   @"responseIsRegisteredServices.registeredToServiceMusic" : @"registeredToServiceMusic", 
                   @"responseIsRegisteredServices.registeredToServiceSong" : @"registeredToServiceSong", 
                   @"responseIsRegisteredServices.registeredToServiceTv" : @"registeredToServiceTv", }]; 

享受。