我正在尝试将一个简单的数组写入 plist 文件,然后再检索它。我有以下代码:
+ (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;
}
我调用了这个方法两次,第一次它不应该找到文件并第一次写入它。第二次调用应该能够找到该文件,但事实并非如此。谁能看出我哪里出错了?
最佳答案
再一次,Xcode 愚蠢的自动完成让你迷路了:NSDocumentationDirectory
应该真的是 NSDocumentDirectory
。
iOS 上不存在 Documentation 目录,所以你不能在那里写。
关于iphone - 找不到 plist 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15837315/