没有对象类的RestKit映射对象

时间:2014-01-27 09:37:29

标签: ios objective-c 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添加到keyPath:的{​​{1}}而RKResponseDescriptor数组为空时。

提前致谢。

1 个答案:

答案 0 :(得分: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", }];

享受。