0

How to convert custom object(say objectType1) containing some properties and array of another custom object(say objectType2), into dictionary, using using RKObjectMapping.

Basically I need to save a custom object in 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;
}

Consider the example

@interface ObjectType1 : NSObject

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

@interface ObjectType2 : NSObject

@property (nonatomic) NSString * address;

Now I want to get the NSDictionary for a Object of type ObjectType1

1 Answers1

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.

Wain
  • 118,658
  • 15
  • 128
  • 151