Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You can create a category for <code>NSDictionary</code> to do this -- there isn't a standard way in the Cocoa library that I could find either. The code that I use looks like this:</p> <pre><code>// file "NSDictionary+UrlEncoding.h" #import &lt;cocoa/cocoa.h&gt; @interface NSDictionary (UrlEncoding) -(NSString*) urlEncodedString; @end </code></pre> <p>with this implementation:</p> <pre><code>// 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: @"&amp;"]; } @end </code></pre> <p>I think the code's pretty straightforward, but I discuss it in some more detail at <a href="http://blog.ablepear.com/2008/12/urlencoding-category-for-nsdictionary.html" rel="noreferrer">http://blog.ablepear.com/2008/12/urlencoding-category-for-nsdictionary.html</a>.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload