How to save a NSMutableDictionary into a file in documents?
Asked Answered
H

2

1

I would like to save the content of a NSMutableDictionary object to a file. How do I do this ? I already know how to do this task with a NSDictionary object but I don't know how to convert/copy this NSMutableDictionary to a NSDictionary...unless there's a method to write directly the content of NSMutableDictionary to a file...I stress that the NSMutableDictionary object contains objects of NSDictionary type.

Thx for helping,

Stephane

Hunterhunting answered 19/9, 2011 at 23:22 Comment(0)
C
1

NSMutableDictionary is a subclass of NSDictionary: you can use it anywhere you'd use NSDictionary. Literally, just pass the object through to the same code you use for NSDictionary right now.

In a more general sense, if you ever actually need to get a truly immutable NSDictionary from an NSMutableDictionary, just call copy on the NSMutableDictionary.

Other approaches include [NSDictionary dictionaryWithDictionary:] or [NSDictionary alloc] initWithDictionary:], which all amount to essentially the same thing.

Catenary answered 19/9, 2011 at 23:27 Comment(3)
I'm not sure the writeToFile:atomically will work directly on a NSMutableDictionary object. It's just an example showing that we cant use a NSMutableDictionary wherever we use a NSDictionaryHunterhunting
It absolutely will work. Why would you think otherwise? There is no operation I can think of that you'd make to an NSDictionary that would fail when attempted on an NSMutableDictionary. Regardless, as I mentioned, you can use [myMutableDict copy] or [NSDictionary dictionaryWithDictionary:myMutableDict] to go from one type to the other if you have reason to.Catenary
copy of NSMutableDictionary doesn't result in true NSdictionary, because of you save the copy of NSMutableDictionary into NSUserdefaults, the saving fails, because it is not a property value (NSDictionary(Silsbye
M
1

If you just swing over to the NSDictionary documents. You will see there is a method for saving a dictionary to a file

writeToFile:atomically:

Writes a property list representation of the contents of the dictionary to a given path.

- (BOOL)writeToFile:(NSString *)path atomically:(BOOL)flag

Parameters

path: The path at which to write the file. If path contains a tilde (~) character, you must expand it with stringByExpandingTildeInPath before invoking this method.

flag: A flag that specifies whether the file should be written atomically.

If flag is YES, the dictionary is written to an auxiliary file, and then the auxiliary file is renamed to path. If flag is NO, the dictionary is written directly to path. The YES option guarantees that path, if it exists at all, won’t be corrupted even if the system should crash during writing.

Return Value

YES if the file is written successfully, otherwise NO.

This method recursively validates that all the contained objects are property list objects (instances of NSData, NSDate, NSNumber, NSString, NSArray, or NSDictionary) before writing out the file, and returns NO if all the objects are not property list objects, since the resultant file would not be a valid property list.

If the dictionary’s contents are all property list objects, the file written by this method can be used to initialize a new dictionary with the class method dictionaryWithContentsOfFile: or the instance method initWithContentsOfFile:.

So the piece of code you are looking for is probably something like:

[myDict writeToFile:path atomically:YES]

where myDict is the dictionary you have and path is the path to the location you want to save it to

Manille answered 19/9, 2011 at 23:37 Comment(2)
I already know this method. What I'm asking is a way to convert a NSMutableDictionary to a NSDictionary to call the writeToFile:atomically methodHunterhunting
You don't need to cast it... NSMutableDictionary is a child of NSDicitonary. All functions that work on an NSDictionary will work on an NSMutableDictionary. Do not get confused though, this is a one way street. Functions that take a NSMutableDicitonary MAY NOT work on a NSDicitonary.Manille

© 2022 - 2024 — McMap. All rights reserved.