19

I want to save an NSMutableArray or NSDictionary as a permanent file. Then, I would like to reopen the file later.

Is this possible? If so, how can I do it?

lukasgeiter
  • 147,337
  • 26
  • 332
  • 270
RAGOpoR
  • 8,118
  • 19
  • 64
  • 97

2 Answers2

52

To write in file

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:FILE_NAME];

[myArray writeToFile:filePath atomically:YES];

To read from file

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:FILE_NAME];

myArray = [NSArray arrayWithContentsOfFile:filePath];

myArray will be nil if file is not present. And NSDictionary has similar methods. Please check the reference for the details of these methods.

Umar Farooque
  • 2,049
  • 21
  • 32
taskinoor
  • 45,586
  • 12
  • 116
  • 142
  • 4
    @gef, is there any practical difference between TRUE/FALSE and YES/NO? – taskinoor May 22 '11 at 19:01
  • 2
    @taskinoor hi, how should be `FILE_NAME` be formatted? can you give me an example what file name can be used. – Bazinga Oct 25 '12 at 01:41
  • @Superman, FILE_NAME is any valid file name. There is no special formatting. – taskinoor Oct 26 '12 at 12:16
  • Because I tried your answer on my problem, doesnt seem to work. cant find it in the directory, – Bazinga Oct 26 '12 at 13:28
  • I am not being able to Find the path where the File is saved.. Help? – yabtzey Aug 19 '13 at 03:13
  • @Superman, as an example i'm saving an NSDictionary, and appending the file path "/Documents" with "loginDictionary.plist" was my solution. Hope that helps – Joel Balmer Feb 26 '14 at 15:24
  • also, @YugeshShrestha, what i just said might help, not sure. Good luck! – Joel Balmer Feb 26 '14 at 15:25
  • This is not working for me. I am saving an NSArray. The path I am presently saving is @"/Documents/newsfeed.plist" – Katedral Pillon Sep 11 '14 at 00:34
  • Also my array is of a custom object, where the properties of the object in question are strings and numbers. – Katedral Pillon Sep 11 '14 at 00:35
  • The files is never saved. – Katedral Pillon Sep 11 '14 at 00:50
  • @KatedralPillon `@"/Documents/newsfeed.plist"` doesn't look like a valid path. You have to use `NSSearchPathForDirectoriesInDomains`. However, this is a very old answer and I'm not sure whether things have changed in past few years. If you have items that can't be serialised then you probably have to serialise yourself. If you don't know how to do that then you can Google for a solution or ask a new question if Google isn't helpful. – taskinoor Sep 11 '14 at 04:45
2

May be you can convert a Dictionary to JSON data.and write it to a file.

NSDictionary *dict = /* the NSDictionary/NSArray you need to write */

NSError *writeError = nil;
NSData* data;
data = [NSJSONSerialization dataWithJSONObject:dict options:NSJSONWritingPrettyPrinted error:&writeError];

if (!writeError) {
    [data writeToFile:filePath atomically:YES];
}
Hancock_Xu
  • 63
  • 5