MdRiduan
MdRiduan

Reputation: 307

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?

Upvotes: 0

Views: 191

Answers (2)

JZAU
JZAU

Reputation: 3567

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.
}

Upvotes: 1

Andrei Nagy
Andrei Nagy

Reputation: 507

The file isn't created when -dataFilePath is called.

To write a plain string in your file, use:

NSError *e = nil;
[@"blabla" writeToFile:[self dataFilePath] atomically:YES encoding:NSUTF8StringEncoding error:&e];

NSError *e1 = nil;
NSString *fileContents = [NSString stringWithContentsOfFile:[self dataFilePath] encoding:NSUTF8StringEncoding error:&e1];

Both errors should be nil and fileContents should contain the original string.

Upvotes: 0

Related Questions