Reputation: 8508
I'm trying to build my NSDictionnary
to send to a request using the AFNetworking framework, but it seems that I'm quite confused about how to do it properly.
Here's what the server is expecting :
{
"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
}
What I'm trying to do (I don't really know if it's the right way to do it tbh), is to build a NSDictionary like the following :
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];
But I have the following error when the view is loading :
*** Terminating app due to uncaught exception 'NSInvalidArgumentException',
reason: '+[NSDictionary dictionaryWithObjectsAndKeys:]: second object of each pair must be non-nil
I know I screw up building the parameters properly, but I can't manage to do it after several tries. Could anyone help ? Moreover, I don't really know the differences that I must implement here with the []
and the {}
. I read that {}
was for a dictionary and []
for an array, but I don't really see how to translate it in my case.
Upvotes: 0
Views: 4145
Reputation: 19544
Your mistake is that the value for the dictionary starting on line 3 needs to be wrapped in an array.
At least until Objective-C array and hash literals go mainstream, my preferred method for creating complex dictionaries is to build them from an NSMutableDictionary
. In your case:
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"];
// ...
Also, be sure you're sending that over as JSON by setting AFJSONParameterEncoding
to your AFHTTPClient
.
Upvotes: 2
Reputation: 55334
The brackets []
signify an array, while the braces {}
signify an object (dictionary in this context). To produce the structure you require:
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];
Upvotes: 2