映射对象数组中的字符串数组

时间:2014-09-07 13:53:53

标签: ios objective-c mapping restkit restkit-0.20

我有这个json对象,我试图映射:

{
    totalresults: 1,
    results: [
              {
                  sources: [
                            "The Blog",
                            "The website",
                            ],
                  description: "Some description."",
                  subjects: [
                             "Education"
                             ],
                  imageurl: "image.jpg",
                  authorid: 44303,
                  istail: false,
                  tailcount: 0,
                  displayname: "Name of me"
              }
          ]
}

我创建了一个SearchPerPage对象:

@interface RestSearchPerPage : NSObject
@property (strong, nonatomic) NSNumber *totalResults;
@property (strong, nonatomic) NSArray *results;
@end

我创建了一个SearchResults对象:

@interface SearchResults : NSObject
@property (strong, nonatomic) NSArray *sources;
@property (strong, nonatomic) NSArray *subjects;
@property (strong, nonatomic) NSString *content;
@property (strong, nonatomic) NSString *imageUrl;
@property (strong, nonatomic) NSString *authorId;
@property (strong, nonatomic) NSString *displayName;
@property (strong, nonatomic) NSNumber *isTail;
@property (strong, nonatomic) NSNumber *tailCount;
@end

我正好映射对象,但是当我试图映射NSArray *sourcesNSArray *subjects时,我总是得到NULL

这是我对资源的最后一次映射尝试:

RKObjectMapping *searchResultsMapping = [RKObjectMapping mappingForClass:[SearchResults class]];
[searchResultsMapping addAttributeMappingsFromDictionary:@{ @"sources" : @"sources",
                                                            @"subjects" : @"subjects",
                                                            @"description" : @"content",
                                                            @"imageurl" : @"imageUrl",
                                                            @"authorid" : @"authorId",
                                                            @"displayname" : @"displayName",
                                                            @"istail" : @"isTail",
                                                            @"tailcount" : @"tailCount" }];

RKObjectMapping *searchPerPageMapping = [RKObjectMapping mappingForClass:[SearchPerPage class]];
[searchPerPageMapping addAttributeMappingsFromDictionary:@{ @"totalresults" : @"totalResults" }];
[searchPerPageMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"results" toKeyPath:@"results" withMapping:searchResultsMapping]];


[[RKObjectManager sharedManager] addResponseDescriptor:[RKResponseDescriptor responseDescriptorWithMapping:searchPerPageMapping
                                                                                                    method:self.method
                                                                                               pathPattern:kGetSearchPerPage
                                                                                                   keyPath:nil
                                                                                               statusCodes:self.statusCode]];

请帮助我,因为我迷路了,只是不知道下一步该做什么 - 对这个问题没有答案。提前谢谢。

2 个答案:

答案 0 :(得分:0)

所以,问题是我没有像那样映射'结果'数组:

[searchPerPageMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"results" toKeyPath:@"results" withMapping:searchResultsMapping]];

我编辑了我的问题并修复了它。

答案 1 :(得分:-1)

您确定要初始化数组,例如

NSArray *subjects = [NSArray array];

忘记这一点很常见:)