Objective c 序列化RestKit(Rails后端)中的嵌套映像
以下是我得到的:Objective c 序列化RestKit(Rails后端)中的嵌套映像,objective-c,ruby-on-rails,ios,multipart,restkit,Objective C,Ruby On Rails,Ios,Multipart,Restkit,以下是我得到的: food_name_token Pizza id 1 category_id 1 review {"note"=>"tasty!", "image"=>"<RKParamsAttachment: 0x7834500>"} 我正在使用Restkit,我正在尝试在Iphone上序列化我的数据,并将其发送到Rails后端。在大多数情况下,它是有效的,但我似乎无法让我的多部分图像序列化 该图像属于“Review”类,而该类又
food_name_token Pizza
id 1
category_id 1
review {"note"=>"tasty!", "image"=>"<RKParamsAttachment: 0x7834500>"}
我正在使用Restkit,我正在尝试在Iphone上序列化我的数据,并将其发送到Rails后端。在大多数情况下,它是有效的,但我似乎无法让我的多部分图像序列化
该图像属于“Review”类,而该类又嵌套在“Dish”类中。以下是我到目前为止的情况:
// Set up routes
RKObjectManager* manager = [RKObjectManager objectManagerWithBaseURL:@"http://postbin.org"];
[manager.router routeClass:[Dish class] toResourcePath:@"/5a850fe1" forMethod:RKRequestMethodPOST];
[manager.router routeClass:[Review class] toResourcePath:@"/5a850fe1" forMethod:RKRequestMethodPOST];
// Map Dish Object
RKObjectMapping *dishMapping = [RKObjectMapping mappingForClass:[NSMutableDictionary class]];
[dishMapping mapKeyPath:@"id" toAttribute:@"identifier"];
[dishMapping mapKeyPath:@"category_id" toAttribute:@"categoryID"];
[dishMapping mapKeyPath:@"food_name_token" toAttribute:@"name"];
// Map Review Object
RKObjectMapping *reviewMapping = [RKObjectMapping mappingForClass:[Review class]];
[reviewMapping mapKeyPath:@"note" toAttribute:@"note"];
[reviewMapping mapKeyPath:@"image" toAttribute:@"image"];
// Define the relationship mapping
[dishMapping mapKeyPath:@"review" toRelationship:@"review" withMapping:reviewMapping];
// Set serialization for Dish Class
RKObjectMapping* dishSerializationMapping = [dishMapping inverseMapping];
[[RKObjectManager sharedManager].mappingProvider setSerializationMapping:dishSerializationMapping forClass:[Dish class] ];
// Set serialization for Review Class
RKObjectMapping* reviewSerializationMapping = [reviewMapping inverseMapping];
[[RKObjectManager sharedManager].mappingProvider setSerializationMapping:reviewSerializationMapping forClass:[Review class] ];
然后我分配值:
[Dish sharedDish].name = @"Pizza";
[Dish sharedDish].identifier = [NSNumber numberWithInt:1];
[Dish sharedDish].categoryID = [NSNumber numberWithInt:1];
[Review sharedReview].note = @"tasty!";
// Turn an image into an RKParamsAttachment object and assign it to the image attribute of the Review class
RKParams* params = [RKParams params];
UIImage* imageOrig = [UIImage imageNamed:@"pizza.jpg"];
NSData* imageData = UIImagePNGRepresentation(imageOrig);
RKParamsAttachment* image = [params setData:imageData MIMEType:@"image/png" forParam:@"image"];
[params release];
[Review sharedReview].image = image;
然后我将我的审阅对象指定给我的Dish对象的审阅属性,并将其发送:
[Dish sharedDish].review = [Review sharedReview];
[[RKObjectManager sharedManager] postObject:[Dish sharedDish] delegate:self];
看起来我把图像附加到对象上的方式不对
正确的方法是什么?是否尝试手动将嵌套添加到参数名称:
RKParamsAttachment* image = [params setData:imageData
MIMEType:@"image/png" forParam:@"review[image]"];
看来你删除了你以前的答案。我认为您以不受支持的方式使用RKParamsAttachment。RestKit RKCatalog示例项目中有一个RKParamsAttachment示例。你试过了吗?是的,我引用了那个例子。问题是我不知道如何正确使用这一行:
[params setData:imageData MIMEType:@“image/png”forParam:@“image”]
我需要将类似{review=>image}
的内容分配给'forParam'参数,而不仅仅是@“image”
,因为image属性属于嵌套在'Dish'对象中的'review'对象。但我要发布的是“Dish”对象。您知道'forParam'参数是否可以接受超过一级深度的密钥吗?这可能会解决问题。。。
RKParamsAttachment* image = [params setData:imageData
MIMEType:@"image/png" forParam:@"review[image]"];