0

如何使用 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

4

1 回答 1

0

You aren't making any network requests as far as I can tell so you don't need a request descriptor. You're just trying to locally convert a graph of model objects into JSON so you should be using RKMapperOperation.

于 2016-02-11T11:01:06.553 回答