// Convert a NS Dictionary into Params
RKParams *params = [RKParams paramsWithDictionary:optionValues];
// I use sendObject to skip the router. Otherwise it's normally postObject
[[RKObjectManager sharedManager] sendObject:yourObject toResourcePath: yourResourcePath usingBlock:^(RKObjectLoader *loader) {
loader.method = RKRequestMethodPOST;
loader.delegate = delegate;
loader.params = params; // This sets params in the POST body and discards your yourObject mapping
} ];
买者自负(上面)
中块设置PARAMS破坏,你可能在yourObject
已设置的任何映射,这种打破了使用对象映射的目的。如果你真的想用这种方法在你的Post中附加额外的参数而不是在对象中,Sebastian loader.params - Extra params有一个修正。
params中发送作为查询字符串(即GET
)
// Make a NS dictionary and use stringByAppendingQueryParameters
NSDictionary *shopParams = [NSDictionary dictionaryWithKeysAndObjects:
@"limit",@"20",
@"location",@"latitude,longitude",
nil];
[[RKObjectManager sharedManager] loadObjectsAtResourcePath:[@"/api/v1/shops.json" stringByAppendingQueryParameters:shopParams] delegate:objectDelegate];
答案的其余部分是仅供参考,我是囤积。
老回答
我使用RestKit为我的项目,面临着同样的问题。
我认为RKParams
主要用于做POST
请求。我不能完全破译你的代码,因为1)我不知道loader
的声明? 2)RKParams
不可与Object Manager
一起使用?
我做到了。
装载机法在应用程序委托
NSDictionary *shopParams = [NSDictionary dictionaryWithKeysAndObjects:@"limit",@"30", nil];
[[RKClient sharedClient] get:@"/api/v1/shops.json" queryParams:shopParams delegate:self];
代表
- (void)requestDidStartLoad:(RKRequest *)request {
NSLog(@"RK Request description: %@",[request description]);
}
输出: RK Request description: <RKRequest: 0x7993db0>
和轨道记录说{"limit"=>"30"}
。
从Xcode中的自动完成,你可以看到get
请求甚至没有使用RKParams
。只是一个NSDict
。 POST请求使用它。
我的目标是将一个查询字符串,即?location=singapore&etcetc
附加到Rails中的API方法中。为此,RK附带了一个名为appendQueryParams
RK docs link的NSString
插件,您可以使用它追加查询参数。
如果你的目标是POST
图片等,你可以按照上述思路使用RKClient
。
更新:
如果你只是想附加参数对象管理
NSDictionary *shopParams = [NSDictionary dictionaryWithKeysAndObjects:
@"limit",@"20",
@"location",@"latitude,longitude",
nil];
这是过时的,标记为弃用。
[[RKObjectManager sharedManager] loadObjectsAtResourcePath:[@"/api/v1/shops.json" appendQueryParams:shopParams] delegate:self];
使用这个代替:
[[RKObjectManager sharedManager] loadObjectsAtResourcePath:[@"/api/v1/shops.json stringByAppendingQueryParameters:shopParams] delegate:yourLoaderDelegate];
Rails的日志:{"location"=>"latitude,longitude", "limit"=>"20"}
希望我的回答我没有做任何错误陈述。
提到这个问题RestKit GET query parameters。
最终这也是我所做的。然而,我确实想使用对象管理器来使用RestKit的映射功能。 – 2012-02-26 20:38:53
@daemonsy感谢这一堆。一段时间以来,我一直在嘲笑我的头。 – drewish 2012-07-18 15:28:29
@drewish谢谢。每当有人发现这个有用的时候,我会尽量更新它,以便更彻底。 – 2012-07-24 06:05:53