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