0

我正在尝试NSDictionnary使用 AFNetworking 框架构建我的发送到请求,但似乎我对如何正确执行它感到很困惑。

这是服务器所期望的:

{
    "limit":10,
    "filters":
    [
        {"field":"owner","operator":"EQUAL","value":"ownerId","type":"integer"},
        {"field":"date","operator":"GE","value":"30 Jun 2010 00:00:00","type":"date"},
    ],
    "order":[{"field":"date","order":"ASC"}],
    "page":0
}

我正在尝试做的(我真的不知道这是否是正确的方法),是构建一个如下所示的 NSDictionary:

NSDictionary *parameters = [NSDictionary dictionaryWithObjectsAndKeys:
                                @"10", @"chunkSize", 
                                [NSDictionary dictionaryWithObjectsAndKeys:
                                    [NSDictionary dictionaryWithObjectsAndKeys:@"owner", @"field", @"EQUAL", @"operator", @"ownerId", @"value", @"integer", @"type", nil],
                                    [NSDictionary dictionaryWithObjectsAndKeys:@"date", @"field", @"GE", @"operator", @"30 Jun 2010 00:00:00", @"value", @"date", @"type", nil],
                                    nil], @"filters",
                                [NSDictionary dictionaryWithObjectsAndKeys:
                                    [NSDictionary dictionaryWithObjectsAndKeys:@"date", @"field", @"ASC", @"order", nil],
                                    nil], @"order",
                                @"0", @"page",
                                nil];

但是在加载视图时出现以下错误:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', 
reason: '+[NSDictionary dictionaryWithObjectsAndKeys:]: second object of each pair must be non-nil

我知道我正确地构建了参数,但经过几次尝试后我无法做到。有人可以帮忙吗?此外,我真的不知道我必须在这里与[]和实现的区别{}。我读到这{}是一本字典和[]一个数组,但我真的不知道如何翻译它。

4

2 回答 2

2

括号[]表示一个数组,而大括号{}表示一个对象(在此上下文中为字典)。要生成您需要的结构:

NSDictionary *parameters = [NSDictionary dictionaryWithObjectsAndKeys:
    @"10", @"chunkSize", 
    [NSArray arrayWithObjects:
         [NSDictionary dictionaryWithObjectsAndKeys:@"owner", @"field", @"EQUAL", @"operator", @"ownerId", @"value", @"integer", @"type", nil],
         [NSDictionary dictionaryWithObjectsAndKeys:@"date", @"field", @"GE", @"operator", @"30 Jun 2010 00:00:00", @"value", @"date", @"type", nil],
         nil], @"filters",
    [NSArray arrayWithObjects:
         [NSDictionary dictionaryWithObjectsAndKeys:@"date", @"field", @"ASC", @"order", nil],
         nil], @"order",
    @"0", @"page",
    nil];
于 2012-07-11T14:20:25.083 回答
2

您的错误是从第 3 行开始的字典的值需要包装在一个数组中。

至少在 Objective-C数组和哈希字面量成为主流之前,我创建复杂字典的首选方法是从NSMutableDictionary. 在你的情况下:

NSMutableDictionary *mutableParameters = [NSMutableDictionary dictionary];
[mutableParameters setValue:@"10" forKey:@"limit"];
// ...

NSMutableArray *mutableFilters = [NSMutableArray array];
NSMutableDictionary *mutableOwnerFilterDictionary = [NSMutableDictionary dictionary];
[mutableOwnerFilterDictionary setValue:@"owner" forKey:@"field"];
// ...
[mutableFilters addObject:mutableOwnerFilterDictionary]; 

[mutableParameters setValue:mutableFilters forKey:@"filters"];
// ...

此外,请确保通过设置AFJSONParameterEncoding为您的AFHTTPClient.

于 2012-07-11T14:21:56.430 回答