Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert NSString to NSDictionary

Is there a way to get an NSDictionary back from the string created via its description method?

Given this code:

NSDictionary *dictionary = [NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:@"value 1", @"value 2", nil] forKeys:[NSArray arrayWithObjects:@"key 1", @"key 2", nil]];
NSString *dictionaryString = [dictionary description];
NSLog(dictionaryString);

Which makes this output:

{
    "key 1" = "value 1";
    "key 2" = "value 2";
}

Is there an easy way to go back from the string to an NSDictionary?

like image 920
Lounges Avatar asked Jun 15 '09 21:06

Lounges


4 Answers

At the risk of asking a stupid question: why would you want to do this? If you want to go to and from text, use the NSPropertyListSerialization class.

like image 193
Ben Gottlieb Avatar answered Nov 18 '22 14:11

Ben Gottlieb


The short answer is no. The description text is intended to be a quick description of the contents of the dictionary, usually for debugging purposes.

It might be possible (but not recommended) to parse the output string and use it to populate a new dictionary, but there are many cases where this would not work. If, for example, your original dictionary contained any custom data types, the description output would appear something like:

{
    "key 1" = <SomeClass 0x31415927>;
    "key 2" = <SomeClass 0x42424242>;
}

Which is, of course, not reversible.

From Apple's developer documentation for the description method in NSDictionary:

If each key in the receiver is an NSString object, the entries are listed in ascending order by key, otherwise the order in which the entries are listed is undefined. This method is intended to produce readable output for debugging purposes, not for serializing data. If you want to store dictionary data for later retrieval, see Property List Programming Guide and Archives and Serializations Programming Guide for Cocoa.

like image 26
e.James Avatar answered Nov 18 '22 15:11

e.James


    NSDictionary *dictionary = [NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:@"value 1", @"value 2", nil] forKeys:[NSArray arrayWithObjects:@"key 1", @"key 2", nil]];
    NSString *dictionaryString = [dictionary description];
    NSLog(@"%@", dictionaryString);

    NSDictionary *d = [dictionaryString propertyList];
    NSLog(@"%@", [d objectForKey:@"key 2"]);

Note that Apple's docs say

/* These methods are no longer recommended since they do not work with property lists and strings files in binary plist format. Please use the APIs in NSPropertyList.h instead. */

However, if you know or can validate that you're not passing a binary format, this should work without issue.

like image 39
applehelpwriter Avatar answered Nov 18 '22 15:11

applehelpwriter


I don't think there is one, you can write a method yourself through, by enumerating through the contents of a string, it looks like a pretty easy format to go through.

like image 1
SuPra Avatar answered Nov 18 '22 13:11

SuPra