From 71bb48fd16b18808504a57e0946ed938254d1fa9 Mon Sep 17 00:00:00 2001 From: Mattt Thompson Date: Wed, 18 Jan 2012 17:57:26 -0800 Subject: [PATCH] Adding documentation for array formatting behavior for AFQueryStringFromParametersWithEncoding Minor refactoring --- AFNetworking/AFHTTPClient.h | 5 ++++- AFNetworking/AFHTTPClient.m | 14 +++++++------- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/AFNetworking/AFHTTPClient.h b/AFNetworking/AFHTTPClient.h index cb560f4..62fa457 100644 --- a/AFNetworking/AFHTTPClient.h +++ b/AFNetworking/AFHTTPClient.h @@ -53,7 +53,10 @@ extern NSString * AFURLEncodedStringFromStringWithEncoding(NSString *string, NSS @param parameters The parameters used to construct the query string @param encoding The encoding to use in constructing the query string. If you are uncertain of the correct encoding, you should use UTF-8 (NSUTF8StringEncoding), which is the encoding designated by RFC 3986 as the correct encoding for use in URLs. - @discussion Query strings are constructed by collecting each key-value pair, URL-encoding the string value of the key and value (by sending `-description` to each), constructing a string in the form "key=value", and then joining the components with "&". The constructed query string does not include the ? character used to delimit the query component. + @discussion Query strings are constructed by collecting each key-value pair, URL-encoding a string representation of the key-value pair, and then joining the components with "&". + + + If a key-value pair has a an `NSArray` for its value, each member of the array will be represented in the format `key[]=value1&key[]value2`. Otherwise, the key-value pair will be formatted as "key=value". String representations of both keys and values are derived using the `-description` method. The constructed query string does not include the ? character used to delimit the query component. @return A URL-encoded query string */ diff --git a/AFNetworking/AFHTTPClient.m b/AFNetworking/AFHTTPClient.m index de4d625..e7eaa7e 100644 --- a/AFNetworking/AFHTTPClient.m +++ b/AFNetworking/AFHTTPClient.m @@ -89,19 +89,19 @@ NSString * AFURLEncodedStringFromStringWithEncoding(NSString *string, NSStringEn NSString * AFQueryStringFromParametersWithEncoding(NSDictionary *parameters, NSStringEncoding encoding) { NSMutableArray *mutableParameterComponents = [NSMutableArray array]; for (id key in [parameters allKeys]) { - id arg = [parameters valueForKey:key]; - if ([arg isKindOfClass:[NSArray class]]) { - // For arrays, we add each item to the query string - NSString *keyComponent = AFURLEncodedStringFromStringWithEncoding([NSString stringWithFormat:@"%@[]", [key description]], encoding); - for (id obj in (NSArray *)arg) { - NSString *component = [NSString stringWithFormat:@"%@=%@", keyComponent, AFURLEncodedStringFromStringWithEncoding([obj description], encoding)]; + id value = [parameters valueForKey:key]; + if ([value isKindOfClass:[NSArray class]]) { + NSString *arrayKey = AFURLEncodedStringFromStringWithEncoding([NSString stringWithFormat:@"%@[]", [key description]], encoding); + for (id arrayValue in value) { + NSString *component = [NSString stringWithFormat:@"%@=%@", arrayKey, AFURLEncodedStringFromStringWithEncoding([arrayValue description], encoding)]; [mutableParameterComponents addObject:component]; } } else { - NSString *component = [NSString stringWithFormat:@"%@=%@", AFURLEncodedStringFromStringWithEncoding([key description], encoding), AFURLEncodedStringFromStringWithEncoding([arg description], encoding)]; + NSString *component = [NSString stringWithFormat:@"%@=%@", AFURLEncodedStringFromStringWithEncoding([key description], encoding), AFURLEncodedStringFromStringWithEncoding([value description], encoding)]; [mutableParameterComponents addObject:component]; } } + return [mutableParameterComponents componentsJoinedByString:@"&"]; }