objective-c - 使用 RestKit/iOS 映射嵌套数组

标签 objective-c ios json ios6 restkit

我需要用 RestKit 映射一个嵌套数组,我几乎已经搞定了(我想)。

我需要映射的 JSON,看起来像这样

[
    {
        _id: "5058670183970a0002000450",
        app_store_url: "http://itunes.apple.com/app/culturonda-alto-adige-sudtirol/id534467629",
        comments: [
            {
                _id: "5058670783970a0002000456",
            },
            {
                _id: "50588d7f83970a000200065c",
            }
        ]
    }
]

我用来存储信息的 Controller 是这样的:

#import <Foundation/Foundation.h>
#import "CommentData.h"

@interface DesignData : NSObject
    @property (retain, nonatomic) NSString *designId;
    @property (retain, nonatomic) NSSet *commentsRelationship;
@end

和:

#import <Foundation/Foundation.h>

@interface CommentData : NSObject
    @property (retain, nonatomic) NSString *commentId;
@end

我的映射看起来像这样:

RKObjectManager *objectManager = [RKObjectManager sharedManager];

RKObjectMapping *commentsMapping = [RKObjectMapping mappingForClass:[CommentData class]];
[commentsMapping mapKeyPathsToAttributes:@"_id", @"commentId", nil];

RKObjectMapping *designMapping = [RKObjectMapping mappingForClass:[DesignData class] ];
[designMapping mapKeyPathsToAttributes:@"_id", @"designId", nil];

[designMapping mapKeyPath:@"comments" toRelationship:@"commentsRelationship" withMapping:commentsMapping];

[objectManager.mappingProvider setMapping:designMapping forKeyPath:@""];

评论进入一个数组,但它不会存储在评论 Controller 中。如果我像这样在 objecloader 回调中显示评论:

DesignData *designData = [objects objectAtIndex:0];
NSLog(@"Loaded %@ ", designData.commentsRelationship);

它给了我:

2012-10-20 22:37:55.258 RestKitTest5[4144:c07] Loaded {(
                                                        <CommentData: 0x8689730>,
                                                        <CommentData: 0x868bed0>,
                                                        <CommentData: 0x868bfe0>,
                                                        <CommentData: 0x868bf50>
                                                        )}

这是一个开始。但我做不到:

DesignData *designData = [objects objectAtIndex:0];
NSLog(@"Loaded %@ ", designData.commentsRelationship.commentId);

那么如何将整个事物存储为数组?

最佳答案

我替换这条线后为我工作:

[designMapping mapKeyPath:@"comments" toRelationship:@"commentsRelationship" withMapping:commentsMapping];

与:

[designMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"comments" toKeyPath:@"commentsRelationship" withMapping: commentsMapping]];

关于objective-c - 使用 RestKit/iOS 映射嵌套数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12994462/

相关文章:

ios - 使用 Swift 在 iOS 上无限滚动

ios - 如何segue UIImage?

c# - 从 mailchimp campaignEmailStatsAIMAll 使用 C# 中的 JSON

iphone - 如何在 iOS 中对黑色透明图像进行颜色填充?

ios - Objective-C 中的 "accessors"是什么?

ios 7 UINavigationBar 在使用自动布局时位于内容之上

java - Android 应用程序在请求 HttpResponse<JsonNode> 时崩溃

json - 如何将两个不同的对象传递给 postman 中的 POST 请求以测试 RESTful API?

ios - MyObject *objectOne = (MyObject *)recognizer.view;

ios - 如何设置可在应用程序委托(delegate)中访问的 XCUItest 类的参数