映射父对象数组中的对象数组

时间:2014-08-06 08:05:16

标签: ios objective-c restkit

我无法通过RestKit的映射功能从JSON映射父对象(请求)数组中的对象数组(注释)。

我的所有数据都正常返回,但由于某种原因,注释对象永远不会填充!

请参阅下面的代码:

request.json:

{
    "data": {
        "priorityRequests": [
            {
                "id": 123456,
                "title": "Request 1",
                "comments": [
                    {
                        "author": "John Smith",
                        "content": "This is a comment"
                    }, {
                        "author": "Jane Smith",
                        "content": "This is another comment"
                    }
                ]
            }, {
                "id": 654321,
                "title": "Request 2",
                "comments": [
                    {
                        "author": "John Smith",
                        "content": "This is a comment"
                    }, {
                        "author": "Jane Smith",
                        "content": "This is another comment"
                    }
                ]
            }
        ]
    }
}

Comment.h /米

@interface Comment : NSObject

@property ( strong, nonatomic ) NSString *author;
@property ( strong, nonatomic ) NSString *content;

@end

@implementation Comment

@end

Request.h /米

@import "Request.h"

@interface Request : NSObject

@property ( strong, nonatomic ) NSString *id;
@property ( strong, nonatomic ) NSString *title;
@property ( strong, nonatomic ) Comment *comments;

@end

@implementation Request

@end

RequestManager.m代码段

RKObjectMapping *requestMapping = [ RKObjectMapping mappingForClass: [ Request class ] ];
[ requestMapping addAttributeMappingsFromDictionary:@{
    @"id" : @"id",
    @"title" : @"versionNumber"
}];

RKObjectMapping *commentMapping = [ RKObjectMapping mappingForClass: [ Comment class ] ];
[ commentMapping addAttributeMappingsFromDictionary:@{
    @"title": @"title",
    @"author": @"author"
}];

// Failed attempt 1:
[ requestMapping addPropertyMapping: [ RKRelationshipMapping 
    relationshipMappingFromKeyPath: @"comments"
    toKeyPath: @"comments"
    withMapping: commentMapping ] 
];
// end

// Failed attempt 2:
RKRelationshipMapping* requests_comments = [ RKRelationshipMapping 
    relationshipMappingFromKeyPath: @"comments" 
    toKeyPath: @"comments" 
    withMapping: commentMapping 
];

[ requestMapping addPropertyMapping: requests_comments ];
// end

RequestCommunicator.m代码段

NSDictionary *mappingsDictionary = @{ "data.priorityRequest" : requestMapping };

RKMapperOperation *mapper = [ [ RKMapperOperation alloc ] 
    initWithRepresentation: parsedData // parsed json as above
    mappingsDictionary: mappingsDictionary 
];

NSError *mappingError = nil;

BOOL isMapped = [ mapper execute: &mappingError ];

// If no errors, returned array of mapped objects
if (isMapped && !mappingError) {

    // All data except for comments here
    // _comments = (Comment *) nil
    [ self.delegate receivedResponseObject: [ mapper mappingResult ].array ];

    ... etc.

1 个答案:

答案 0 :(得分:1)

我找到了解决这个问题的方法,虽然可能不是每个人都喝茶,但希望它可以帮助其他人走上正轨。

在我的请求NSObject中,我更改了类型' Comment'到' NSArray':

- @property ( strong, nonatomic ) Comment *comments;
+ @property ( strong, nonatomic ) NSArray *comments;