0

I have NSString representation of NSDictionary

I created the string like that:

 NSString * insertionStr = [dictionary description];

Now I want to convert it back to NSDictionary

Is it possible ?

Coldsteel48
  • 3,482
  • 4
  • 26
  • 43
  • That doesn't "convert" the dictionary - it still is one. All you've done is create an NSString representation of it. Where do you declare the "dictionary" variable? Is that a local variable? A @property? – mc01 Aug 06 '14 at 20:12
  • Please elaborate why you want to do this. I honestly doubt this is the way to go about it. From Apple's documentation of the description method *This method is intended to produce readable output for debugging purposes, not for serializing data.* If you wish to serialize the dictionary and store it in a file, see the property list programming guide or archive and unarchive the dictionary. – Lev Landau Aug 06 '14 at 20:15
  • You don't have a "string which is a dictionary" at all. It's not possible as it was never a dictionary in the first place. – Droppy Aug 06 '14 at 20:16
  • @LevLandau i have tried to store that dictionary in `NSUSerDefaults` but it blamed me with errors :( , so I stored the representation of it as `NSString` and now I want to extract it back as a dictionary – Coldsteel48 Aug 06 '14 at 20:22
  • @mc01 dictionary is a local variable – Coldsteel48 Aug 06 '14 at 20:23
  • @Droppy I have an `NSString` representation of `NSDictionary` and `dictionary` is an instance of `NSDictionary` which is actually dictionary` – Coldsteel48 Aug 06 '14 at 20:24
  • So `[NSUserDefaults setObject:dict forKey:@"MyDict"]` didn't work? – Droppy Aug 06 '14 at 20:26
  • @Droppy no , it claimed that the dictionary is too complicated or something like that , i have googled it , and found that I have to store something like `NSDATA` or `NSString` or something simple :-/ I mean I had runtime error and crash – Coldsteel48 Aug 06 '14 at 20:27
  • @Droppy actually I trying to store an `NSArray` of `NSDictionaries` it didn't allow me , but when I storing an `NSArray` of `strings` like that it does – Coldsteel48 Aug 06 '14 at 20:30
  • If you're going to do this sort of thing, and if the data in the dictionary is "well behaved", you can convert the dictionary to JSON, or (somewhat circuitously) to a "plist". You would do one or the other if you want to preserve the dictionary across application shutdown/resume. – Hot Licks Aug 06 '14 at 20:32
  • (Saving as a "description" is not a good idea -- there is no defined way to "restore" from that format, and it's not even guaranteed to remain unchanged from one iOS version to the next.) – Hot Licks Aug 06 '14 at 20:33
  • @HotLicks Thanks , however I find it sad _ – Coldsteel48 Aug 06 '14 at 20:35
  • Saving as JSON is virtually the same thing (looks almost identical), except that the data contained within the dictionary must be dictionaries, arrays, strings, or NSNumbers -- no other object types. – Hot Licks Aug 06 '14 at 20:41
  • @HotLicks I have done what u suggested and it works , please make an answer , so future users will know what to do thanks . – Coldsteel48 Aug 06 '14 at 20:50

4 Answers4

1

Based on the Question and comments i will say ...NO ...Don't do it like that. NSUserDefaults has the capability to store object values and its perfectly fine to store NSDictionary

[[NSUserDefaults standardUserDefaults] setObject: dictionary forKey:@"DetailDict"];
[[NSUserDefaults standardUserDefaults] synchronize];

and retrive it use

NSDictionary *retrievedDictionary = [[NSUserDefaults standardUserDefaults] dictionaryForKey:@"DetailDict"];

OR

If you think you think of saving it as a string have a look at this question and when retrieving the value just convert back the string to Object

Community
  • 1
  • 1
Lithu T.V
  • 19,955
  • 12
  • 56
  • 101
  • It looks like the OP has objects which cannot be stored in `NSUserDefaults`: http://stackoverflow.com/questions/14282892/nsuserdefaults-fail-to-save-nsmutabledictionary – Droppy Aug 06 '14 at 20:40
  • and the answer to that question explains well enough what to do. – Lithu T.V Aug 06 '14 at 20:46
1

From the NSUserDefaults documentation for setObject:forKey

The value parameter can be only property list objects: NSData, NSString, NSNumber, NSDate, NSArray, or NSDictionary. For NSArray and NSDictionary objects, their contents must be property list objects.

If your dictionary contains contains non-property list objects you can archive and store it to file if all objects in the dictionary implement NSCoding.

Lev Landau
  • 788
  • 3
  • 16
1

A dictionary is represented as key/value pairs. You can create a dictionary with a single value like this:

NSDictionary *dictionary = @{@"name": @"Mike"};

You can also store multiple key/value pairs:

NSDictionary *newDictionary = @{
                                @"firstName": @"Mike",
                                @"lastName": @"Smith"
                               };

Later if you want to get the value back out of the dictionary you use the key to get the value back out of it:

NSString *first = newDictionary[@"firstName"];
NSString *last = newDictionary[@"lastName"];

// first will now be @"Mike" and last will be @"Smith"

Think of a dictionary like an array, but instead of using indexes (numbers) to reference the value you use keys (strings).

Based on a comment made, if you also want to store an array of dictionaries you can do that no problem like this:

NSDictionary *firstDictionary = @{@"key": @"value"};
NSDictionary *secondDictionary = @{@"anotherKey": @"anotherValue"};
NSArray *array = @[firstDictionary, secondDictionary];
Mike
  • 9,765
  • 5
  • 34
  • 59
  • 1
    I'm not sure you understand the question. – Droppy Aug 06 '14 at 20:57
  • The original question? – Mike Aug 06 '14 at 20:58
  • 1
    Well that and the progress made with comments, which is still part of the original question. The user is having issues storing a dictionary in `NSUserDefaults` due to objects which don't conform so he's worked-around it by using a string. Your answer appears to provide no solution. – Droppy Aug 06 '14 at 21:00
  • I didn't read every comment to every response, I addressed the original question which mentions nothing about NSUserDefaults... – Mike Aug 06 '14 at 21:01
1

To save a dictionary to a file (or other medium) and be able to restore it later, you should use either JSON or the plist format.

For JSON your data must be limited to a combination of dictionaries, arrays, strings, and NSNumbers. For a plist it must be one of those or NSDate, or NSData.

For JSON you'd use the methods of NSJSONSerialization to convert to NSData, then save the data to a file. To restore, load the file into NSData and run back through NSJSONSerialization.

For plist format use the NSDictionary writeToFile and dictionaryWithContentsOfFile methods.

Do note that by default the objects you get back are immutable. If you want mutable objects from JSON there is an option on JSONObjectWithData called NSJSONReadingMutableContainers. With plists I believe you need to use the more complex plist interfaces (vs using the simple NSDictionary interfaces) that allow you to specify a similar option.

Hot Licks
  • 47,103
  • 17
  • 93
  • 151