Created
January 8, 2013 09:17
-
-
Save anonymous/4482407 to your computer and use it in GitHub Desktop.
Extends NSdictionary with urlEncodedString
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import <Foundation/Foundation.h> | |
@interface NSDictionary (URLEncoding) | |
-(NSString*) urlEncodedString; | |
@end | |
// helper function: get the string form of any object | |
static NSString *toString(id object) { | |
return [NSString stringWithFormat: @"%@", object]; | |
} | |
// helper function: get the url encoded string form of any object | |
static NSString *urlEncode(id object) { | |
NSString *string = toString(object); | |
return [string stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding]; | |
} | |
@implementation NSDictionary (URLEncoding) | |
-(NSString*) urlEncodedString { | |
NSMutableArray *parts = [NSMutableArray array]; | |
for (id key in self) { | |
id value = [self objectForKey: key]; | |
NSString *part = [NSString stringWithFormat: @"%@=%@", urlEncode(key), urlEncode(value)]; | |
[parts addObject: part]; | |
} | |
return [parts componentsJoinedByString: @"&"]; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment