Adding documentation for array formatting behavior for AFQueryStringFromParametersWithEncoding

Minor refactoring
This commit is contained in:
Mattt Thompson 2012-01-18 17:57:26 -08:00
parent 7d656c2cbe
commit 71bb48fd16
2 changed files with 11 additions and 8 deletions

View file

@ -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
*/

View file

@ -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:@"&"];
}