使用RKObjectMapping进行递归映射?

时间:2016-02-10 19:01:42

标签: ios rkobjectmapping

如何使用RKObjectMapping将包含某些属性和另一个自定义对象数组(例如objectType2)的自定义对象(比如objectType1)转换为字典。

基本上我需要在NSUserDeafults中保存自定义对象。

- (NSDictionary *)dictionaryFromMaping {

RKObjectMapping * itemMapping = [RUserObject mapping];
RKRequestDescriptor *requestDescriptor = [RKRequestDescriptor requestDescriptorWithMapping:itemMapping.inverseMapping objectClass:[self class] rootKeyPath:nil method:RKRequestMethodPOST];
NSDictionary *dictionary = [RKObjectParameterization parametersWithObject:self requestDescriptor:requestDescriptor error:nil];

[dictionary enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
    if (obj == [NSNull null]) {
        [dictionary setValue:nil forKey:key];
    }
}];


return dictionary;
}

考虑示例

@interface ObjectType1 : NSObject

@property (nonatomic) NSString * name;
@property (nonatomic) NSArray * objectType2Array; // array of ObjectType2

@interface ObjectType2 : NSObject

@property (nonatomic) NSString * address;

现在我想获得NSDictionary

类型的对象的ObjectType1

1 个答案:

答案 0 :(得分:0)

据我所知,您没有提出任何网络请求,因此您不需要请求描述符。您只是尝试将模型对象的图形本地转换为JSON,因此您应该使用RKMapperOperation