Reputation: 3512
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 ?
Upvotes: 0
Views: 613
Reputation: 47729
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.
Upvotes: 1
Reputation: 9835
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];
Upvotes: 1
Reputation: 20021
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
Upvotes: 1
Reputation: 808
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
.
Upvotes: 1