Files
RestKit/Code/Support/NSDictionary+RKAdditions.m
2012-05-17 18:10:59 -04:00

123 lines
4.9 KiB
Objective-C

//
// NSDictionary+RKAdditions.m
// RestKit
//
// Created by Blake Watters on 9/5/10.
// Copyright (c) 2009-2012 RestKit. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
#import "NSDictionary+RKAdditions.h"
#import "NSString+RKAdditions.h"
#import "RKFixCategoryBug.h"
RK_FIX_CATEGORY_BUG(NSDictionary_RKAdditions)
@implementation NSDictionary (RKAdditions)
+ (id)dictionaryWithKeysAndObjects:(id)firstKey, ... {
va_list args;
va_start(args, firstKey);
NSMutableArray* keys = [NSMutableArray array];
NSMutableArray* values = [NSMutableArray array];
for (id key = firstKey; key != nil; key = va_arg(args, id)) {
id value = va_arg(args, id);
[keys addObject:key];
[values addObject:value];
}
va_end(args);
return [self dictionaryWithObjects:values forKeys:keys];
}
- (NSDictionary *)dictionaryByReplacingPercentEscapesInEntries {
NSMutableDictionary *results = [NSMutableDictionary dictionaryWithCapacity:[self count]];
[self enumerateKeysAndObjectsUsingBlock:^(id key, id value, BOOL *stop)
{
NSString *escapedKey = [key stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
id escapedValue = value;
if ([value respondsToSelector:@selector(stringByReplacingPercentEscapesUsingEncoding:)])
escapedValue = [value stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
[results setObject:escapedValue forKey:escapedKey];
}];
return results;
}
// TODO: Unit tests...
+ (NSDictionary *)dictionaryWithURLEncodedString:(NSString *)URLEncodedString {
NSMutableDictionary *queryComponents = [NSMutableDictionary dictionary];
for(NSString *keyValuePairString in [URLEncodedString componentsSeparatedByString:@"&"]) {
NSArray *keyValuePairArray = [keyValuePairString componentsSeparatedByString:@"="];
if ([keyValuePairArray count] < 2) continue; // Verify that there is at least one key, and at least one value. Ignore extra = signs
NSString *key = [[keyValuePairArray objectAtIndex:0] stringByReplacingURLEncoding];
NSString *value = [[keyValuePairArray objectAtIndex:1] stringByReplacingURLEncoding];
// URL spec says that multiple values are allowed per key
id results = [queryComponents objectForKey:key];
if(results) {
if ([results isKindOfClass:[NSMutableArray class]]) {
[(NSMutableArray *)results addObject:value];
} else {
// On second occurrence of the key, convert into an array
NSMutableArray *values = [NSMutableArray arrayWithObjects:results, value, nil];
[queryComponents setObject:values forKey:key];
}
} else {
[queryComponents setObject:value forKey:key];
}
[results addObject:value];
}
return queryComponents;
}
- (void)URLEncodePart:(NSMutableArray*)parts path:(NSString*)path value:(id)value {
NSString *encodedPart = [[value description] stringByAddingURLEncoding];
[parts addObject:[NSString stringWithFormat: @"%@=%@", path, encodedPart]];
}
- (void)URLEncodeParts:(NSMutableArray*)parts path:(NSString*)inPath {
[self enumerateKeysAndObjectsUsingBlock:^(id key, id value, BOOL *stop) {
NSString *encodedKey = [[key description] stringByAddingURLEncoding];
NSString *path = inPath ? [inPath stringByAppendingFormat:@"[%@]", encodedKey] : encodedKey;
if ([value isKindOfClass:[NSArray class]]) {
for (id item in value) {
if ([item isKindOfClass:[NSDictionary class]] || [item isKindOfClass:[NSMutableDictionary class]]) {
[item URLEncodeParts:parts path:[path stringByAppendingString:@"[]"]];
} else {
[self URLEncodePart:parts path:[path stringByAppendingString:@"[]"] value:item];
}
}
} else if([value isKindOfClass:[NSDictionary class]] || [value isKindOfClass:[NSMutableDictionary class]]) {
[value URLEncodeParts:parts path:path];
}
else {
[self URLEncodePart:parts path:path value:value];
}
}];
}
- (NSString *)stringWithURLEncodedEntries {
NSMutableArray* parts = [NSMutableArray array];
[self URLEncodeParts:parts path:nil];
return [parts componentsJoinedByString:@"&"];
}
- (NSString *)URLEncodedString {
return [self stringWithURLEncodedEntries];
}
@end