OGeek|极客世界-中国程序员成长平台

标题: iphone - 从 iOS 发布到 Rails 应用程序时序列化嵌套对象 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-11 20:30
标题: iphone - 从 iOS 发布到 Rails 应用程序时序列化嵌套对象

希望在正确的方向上得到一点插入。当我使用 RestKit 发布到我的 rails 应用程序时,我无法让嵌套对象正确序列化。我有以下映射:

RKObjectMapping *cartSerializationMapping = [RKObjectMapping mappingForClass:[TOCart class]];
[cartSerializationMapping mapKeyPath"place.placeID" toAttribute"order[external_id]"];

//map the line items serialization mapping
RKObjectMapping *lineItemSerializationMapping = [RKObjectMapping mappingForClass:[TOLineItem class]];
[lineItemSerializationMapping mapKeyPath"itemID" toAttribute"itemID"];
[lineItemSerializationMapping mapKeyPath"name" toAttribute"name"];

[[RKObjectManager sharedManager].mappingProvider setSerializationMapping:lineItemSerializationMapping forClass:[TOLineItem class]];

//add relationship bw line items to TOLineItem
[cartSerializationMapping mapKeyPath"line_items" toRelationship"order[line_items]" withMapping:lineItemSerializationMapping serialize:YES];

[[RKObjectManager sharedManager].mappingProvider setSerializationMapping:cartSerializationMapping forClass:[TOCart class]];

发布到服务器后,序列化适用于父对象,但不适用于嵌套的 line_item 对象:

Started POST "/orders" for 127.0.0.1 at 2011-11-16 04:05:58 -0800
Processing by OrdersController#create as JSON
Parameters: {"order"=>{"line_items"=>["<TOLineItem: 0x8aafdb0>"], "external_id"=>"4ae8a535f964a52024b121e3"}}

我希望 line_item 序列化为 itemID 和 name 等...

我的映射设置有误吗?

谢谢!

更新:

我的 TOCart 类:

#import <Foundation/Foundation.h>
@class TOPlace;

@interface TOCart : NSObject
{
    NSNumber *cartID;
    TOPlace *place; //post to external id
    NSString *state;
    NSMutableArray *line_items;
}
@property (nonatomic, retain) NSNumber *cartID;
@property (nonatomic, retain) TOPlace *place;
@property (nonatomic, retain) NSString *state;
@property (nonatomic, retain) NSMutableArray *line_items;

@end



Best Answer-推荐答案


我总是将我的映射定义为从 API 映射到实体,然后使用 [myMappingFromApi inverseMapping] 选择器创建序列化映射。您可以在我对有些不同但绝对相关的问题的回答中找到更多详细信息:RestKit: How does one post an array of objects? .

关于iphone - 从 iOS 发布到 Rails 应用程序时序列化嵌套对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8151532/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4