Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/110.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c iOS:RestKit loadObject&;发送参数_Objective C_Ios_Parameters_Restkit - Fatal编程技术网

Objective c iOS:RestKit loadObject&;发送参数

Objective c iOS:RestKit loadObject&;发送参数,objective-c,ios,parameters,restkit,Objective C,Ios,Parameters,Restkit,使用GET方法上的loadObjectAtResourcePath时,不会在请求中包含我的参数 例如,如果我发送: [RKObjectManager objectManagerWithBaseURL:@"http://something/ws"]; [[RKObjectManager sharedManager] loadObjectsAtResourcePath:@"/res" delegate:self block:^(RKObjectLoader *loader) {

使用GET方法上的loadObjectAtResourcePath时,不会在请求中包含我的参数

例如,如果我发送:

[RKObjectManager objectManagerWithBaseURL:@"http://something/ws"];
    [[RKObjectManager sharedManager] loadObjectsAtResourcePath:@"/res" delegate:self block:^(RKObjectLoader *loader) {
        NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:
                              @"val", @"param1",
                              nil];
        loader.params = [RKParams paramsWithDictionary:dict];
    }];

最终的url请求不包括“param1=val”部分-为什么?

几个月后更新

 NSDictionary *shopParams = [NSDictionary dictionaryWithKeysAndObjects:
                                @"limit",@"20", 
                                @"location",@"latitude,longitude",
                                nil];
真正的答案是
loader.params
创建
HTTP正文
,因此它适用于
POST、PUT、DELETE
等,但不适用于
GET
,其中参数附加到URL。

因此,如果您在
GET
中遇到同样的问题,下面的答案仍然有效,但是如果您发送
GET
请求,则主要使用将参数附加到查询字符串的方法

总结两者之间的差异

在HTTP正文中发送参数(即
发布、更新、删除

买主须知(适用于上述内容)

在块中设置参数会破坏您可能在
yourObject
中设置的任何映射,这有点违背了使用对象映射的目的。如果您真的想使用此方法将额外参数附加到Post而不是对象中,Sebastian对此进行了修复

将参数作为查询字符串发送(即
GET

其余答案仅供参考,我是个囤积者


旧答案

 NSDictionary *shopParams = [NSDictionary dictionaryWithKeysAndObjects:
                                @"limit",@"20", 
                                @"location",@"latitude,longitude",
                                nil];
我在我的项目中使用RestKit,面临着同样的问题

我认为
RKParams
主要用于执行
POST
请求。我无法完全破译您的代码,因为1)我不知道加载程序的声明?2)
RKParams
不能与
对象管理器一起使用

是我干的

应用程序委托中的加载程序方法

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]);
}
代表

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请求描述:
和rails日志说
{“limit”=>“30”}

从Xcode中的自动完成功能中,您可以看到
get
请求甚至没有使用
RKParams
。只是一个
NSDict
。POST请求使用它

我的目标是将查询字符串,即
?location=singapore&etcetc
附加到Rails中的API方法。为此,RK附带了一个名为
appendQueryParams
NSString
插件,可用于附加查询参数

如果您的目标是
POST
images等,您可以按照上述思路使用
RKClient

更新:

 [[RKObjectManager sharedManager] loadObjectsAtResourcePath:[@"/api/v1/shops.json stringByAppendingQueryParameters:shopParams] delegate:yourLoaderDelegate];
如果只想将参数附加到对象管理器

 NSDictionary *shopParams = [NSDictionary dictionaryWithKeysAndObjects:
                                @"limit",@"20", 
                                @"location",@"latitude,longitude",
                                nil];
这已过时,并标记为弃用。



改用这个:

 [[RKObjectManager sharedManager] loadObjectsAtResourcePath:[@"/api/v1/shops.json stringByAppendingQueryParameters:shopParams] delegate:yourLoaderDelegate];
轨道日志
{“位置”=>“纬度、经度”、“限制”=>“20”}

希望在我的回答中我没有做出任何错误的陈述


参考这个问题

最终,我也是这么做的。不过,我确实想使用对象管理器来使用RestKit的映射功能。@daemonsy对此非常感谢。“我用头撞它已经有一段时间了。”谢谢。每当有人觉得这个有用的时候,我会尝试更新它,使之更彻底。