James Moore's code blog
Tuesday, February 3, 2009
Building parameters strings for the iPhone from NSDictionary objects
Here's the code I use to build parameters strings for URLs on the iPhone. It adds a method to NSDictionary to convert a dictionary to a parameters string:
@implementation NSString(CallWranglerUtilities) - (NSString *)stringByAddingPercentEscapesForURI { NSString *reserved = @":/?#[]@!$&'()*+,;="; CFStringRef s = CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault, (CFStringRef)self, NULL, (CFStringRef)reserved, kCFStringEncodingUTF8); return [NSMakeCollectable(s) autorelease]; } @end @implementation NSDictionary(CallWranglerUtilities) - (NSString *) convertDictionaryToURIParameterString { NSMutableArray *elements = [NSMutableArray array]; for (NSString *k in [self keyEnumerator]) { NSString *escapedK = [k stringByAddingPercentEscapesForURI]; if (![k isEqualToString: @""]) { NSString *escapedV = [[self objectForKey: k] stringByAddingPercentEscapesForURI]; [elements addObject: [NSString stringWithFormat: @"%@=%@", escapedK, escapedV]]; } } return [elements componentsJoinedByString:@"&"]; } @end
No comments:
Post a Comment
Newer Post
Older Post
Home
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment