Note that there are some explanatory texts on larger screens.

plurals
  1. POConvert NSString to NSDictionary and NSDictionary to NSString
    text
    copied!<p>There are many questions on this already, but the answers given are situation-specific, answering the poster's personal problem rather than the question title.</p> <p>I would like to know if there is a general, universal, easy way to convert an <code>NSString</code> to an <code>NSDictionary</code>, and vice versa?</p> <p>I've got difficult/complex/non-cross-platform approaches, but surely there must be an easier way?</p> <p>Here's what I know / have tested:</p> <ol> <li>Apple provides a <code>Dictionary</code> -> <code>String</code> method that has no inverse, and works perfectly so long as your Dictionary is only basic datatypes, arrays, and dictionaries. This covers most real-world cases, but Apple doesn't give an inverse :(. <ol> <li><code>[myDictionary description];</code></li> <li>// no way back ? Why not?</li> </ol></li> <li>Apple has an intermediate approach using <code>NSData</code> that fails silently, so that it only works in a LIMITED set of real-world cases: <ol> <li><code>[myString dataUsingEncoding:NSUTF8StringEncoding];</code></li> <li><code>[[NSString alloc] initWithData:myData encoding:NSUTF8StringEncoding];</code> // NB: silently fails in many cases, returns nil string</li> </ol></li> <li>Apple has MULTIPLE, INCOMPATIBLE ways of converting <code>NSDictionary</code> -> <code>NSData</code> -> <code>NSDicitonary</code> that work well - but they are using <code>NSData</code>, so they are not a straight conversion, and they are harder to work with <ol> <li>KeyedArchiver: <ol> <li><code>[NSKeyedArchiver archivedDataWithRootObject:myDictionary];</code></li> <li><code>[NSKeyedUnarchiver unarchiveObjectWithData:myData];</code></li> </ol></li> <li>PropertyListSerialization: <ol> <li><code>[NSPropertyListSerialization dataFromPropertyList:plist format:NSPropertyListXMLFormat_v1_0 errorDescription:&amp;error];</code></li> <li><code>[NSPropertyListSerialization propertyListFromData:plistData mutabilityOption:NSPropertyListImmutable format:&amp;format errorDescription:&amp;error];</code></li> </ol></li> <li>NSJSONSerialization: <ol> <li><code>[NSJSONSerialization JSONObjectWithData:myData options:0 error:nil];</code></li> <li><code>[dataWithJSONObject:myDictionary options:0 error:nil];</code></li> </ol></li> </ol></li> </ol> <p>As it stands, the only route that is GUARANTEED to work appears to be:</p> <p>Write lots of code to output a verbose <code>XML</code> or <code>JSON</code>, converting it to <code>NSData</code>, then converting <em>back</em> into <code>NSString</code>, and send that on the wire. </p> <p>Anything else has a slightly-less-than-100% success rate. Unfortunately, this technique turns a very common problem (string -> dictionary -> string) into a multiline chunk of silly boilerplate code we shouldn't be writing.</p> <p>Also ... the <code>XML</code>-or-<code>JSON</code> approach is the only one that correctly reports failures (if they occur) - both the encoding systems take <code>NSError</code> pointers.</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