mirror of
https://github.com/zhigang1992/RestKit.git
synced 2026-01-13 09:30:46 +08:00
78 lines
2.0 KiB
Objective-C
78 lines
2.0 KiB
Objective-C
//
|
|
// NSDictionary+RKRequestSerialization.m
|
|
// RestKit
|
|
//
|
|
// Created by Blake Watters on 7/28/09.
|
|
// Copyright 2009 Two Toasters. All rights reserved.
|
|
//
|
|
|
|
#import "NSDictionary+RKRequestSerialization.h"
|
|
|
|
// private helper function to convert any object to its string representation
|
|
static NSString *toString(id object) {
|
|
return [NSString stringWithFormat: @"%@", object];
|
|
}
|
|
|
|
// private helper function to convert string to UTF-8 and URL encode it
|
|
static NSString *urlEncode(id object) {
|
|
NSString *string = toString(object);
|
|
return [string stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding];
|
|
}
|
|
|
|
|
|
@implementation NSDictionary (RKRequestSerialization)
|
|
|
|
- (NSString*)URLEncodedString {
|
|
NSMutableArray *parts = [NSMutableArray array];
|
|
for (id key in self) {
|
|
id value = [self objectForKey:key];
|
|
if ([value isKindOfClass:[NSArray class]]) {
|
|
for (id item in value) {
|
|
NSString *part = [NSString stringWithFormat: @"%@[]=%@",
|
|
urlEncode(key), urlEncode(item)];
|
|
[parts addObject:part];
|
|
}
|
|
} else {
|
|
NSString *part = [NSString stringWithFormat: @"%@=%@",
|
|
urlEncode(key), urlEncode(value)];
|
|
[parts addObject:part];
|
|
}
|
|
}
|
|
|
|
return [parts componentsJoinedByString: @"&"];
|
|
}
|
|
|
|
- (NSString*)ContentTypeHTTPHeader {
|
|
return @"application/x-www-form-urlencoded";
|
|
}
|
|
|
|
- (NSData*)HTTPBody {
|
|
return [[self URLEncodedString] dataUsingEncoding:NSUTF8StringEncoding];
|
|
}
|
|
|
|
+ (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];
|
|
}
|
|
|
|
- (id)keyForObject:(id)object {
|
|
for (id key in self) {
|
|
if ([[self objectForKey:key] isEqual:object]) {
|
|
return key;
|
|
}
|
|
}
|
|
return nil;
|
|
}
|
|
|
|
@end
|