Files
RestKit/Code/Support/NSDictionary+RKAdditions.m
Blake Watters 214efd31d5 Fix issue with incorrectly built OAuth 1.0 invocation of TDOAuth. fixes #396
Began cleanup of some category methods to eliminate duplicated functionality across the library.
2011-10-12 09:55:38 -04:00

85 lines
3.3 KiB
Objective-C

//
// NSDictionary+RKAdditions.m
// RestKit
//
// Created by Blake Watters on 9/5/10.
// Copyright 2010 Two Toasters
//
// 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+RestKit.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 *)removePercentEscapesFromKeysAndObjects {
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;
}
@end