Last active
August 29, 2015 14:02
-
-
Save BlaShadow/f70d3d58ea1baed8c2da to your computer and use it in GitHub Desktop.
Url encode
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
// file "NSDictionary+UrlEncoding.m" | |
#import "NSDictionary+UrlEncoding.h" | |
// 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: @"&"]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment