objective-cnsdictionarynsmutabledictionary

How to load an NSDictionary from a file created with writeToFile?


I have an NSMutableDictionary, and I wrote it using

[stuff writeToFile:@"TEST" atomically:YES];

How can I retrieve it in the future?

Also, what would happen if I decide to replace my iPhone 4 with the 4S? Can my piece of written data be transferred?


Solution

  • EDIT in 2022/03: changed initWithContentsofFile to initWithContentsOfFile

    I think you want something like:

    [[NSMutableDictionary alloc] initWithContentsOfFile:[self dataFilePath]];
    

    You do need to obtain the correct path to store and retrieve your file, along the lines of this routine:

    - (NSString *)dataFilePath {
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documentsDirectory = [paths objectAtIndex:0];
        return [documentsDirectory stringByAppendingPathComponent:@"TEST"];
    }