Reputation: 14527
I'm trying to write a simple array to a plist file and then later on retrieve it. I have the following code:
+ (NSString*) dataFilePath
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, YES);
NSString *documentDirectory = [paths objectAtIndex:0];
NSString *dataFilePath = [documentDirectory stringByAppendingPathComponent:@"TipAlertViewDefaults.plist"];
return dataFilePath;
}
+ (NSArray*) tipAlertViewDefaults
{
NSString *dataFilePath = [self dataFilePath];
NSLog(@"DataFilePath: %@", dataFilePath);
NSMutableArray *tipAlertViewDefaults;
if ([[NSFileManager defaultManager] fileExistsAtPath:dataFilePath])
{
NSLog(@"File Exists");
tipAlertViewDefaults = [[NSMutableArray alloc] initWithContentsOfFile:dataFilePath];
}
else
{
NSLog(@"File Doesn't Exist");
tipAlertViewDefaults = [[NSMutableArray alloc] initWithObjects:[NSNumber numberWithBool:NO], nil];
[tipAlertViewDefaults writeToFile:dataFilePath atomically:YES];
}
return tipAlertViewDefaults;
}
I call this method twice, in the first it should not find the file and write it for the first time. The second call then should be able to find the file but it isn't. Can anyone see where I'm going wrong here?
Upvotes: 0
Views: 145
Reputation: 530
Do you really want to write in NSDocumentationDirectory
directory? I think, this directory is not created by default (so you must make it before).
Perhaps you wanted NSDocumentDirectory
. I tried it and your code works.
Upvotes: 0
Reputation:
Again, Xcode's stupid autocomplete made you lost: NSDocumentationDirectory
should really be NSDocumentDirectory
.
The Documentation directory doesn't exist on iOS, so you can't write there.
Upvotes: 5