Search code examples
iosplistxcode6nsfilemanager

Can't save and load plist file


When i tried to create plist file('Checklist.plist') using following methods i can't see the file in the directory. enter image description here

- (NSString *)documentsDirectory {
NSArray *paths = NSSearchPathForDirectoriesInDomains( NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths firstObject];
return documentsDirectory; }

- (NSString *)dataFilePath {
return [[self documentsDirectory] stringByAppendingPathComponent:@"Checklists.plist"];
}

Why i can't see the file on the directory? How can i solve this problem?


Solution

  • Did you save something in the file?

    NSString *test = @"test";
    [test writeToFile:[self dataFilePath] atomically:YES];
    

    If you run this in a simulator, you can NSlog this file path, and than open finder, press cmd + shift + g paste the file path, don't include Checklist.plist, just documents file path, you will see the file you just create named Checklist.plist.

    This is all my code:

    - (void)viewDidLoad {
        [super viewDidLoad];
        // Do any additional setup after loading the view, typically from a nib.
        NSArray *testArray = [NSArray arrayWithObjects:@"test", nil];
        [testArray writeToFile:[self dataFilePath] atomically:YES];
    }
    - (NSString *)documentsDirectory {
        NSArray *paths = NSSearchPathForDirectoriesInDomains( NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documentsDirectory = [paths firstObject];
        return documentsDirectory; 
    }
    
    - (NSString *)dataFilePath {
        return [[self documentsDirectory] stringByAppendingPathComponent:@"Checklists.plist"];
    }
    - (void)didReceiveMemoryWarning {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }