没有findplist文件

我试图写一个简单的数组到一个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; } 

我把这个方法调用了两次,第一次找不到这个文件并且第一次写这个文件。 然后第二个电话应该能够find该文件,但它不是。 任何人都可以看到我要去哪里错了吗?

再一次,Xcode的愚蠢的自动完成使你失去了: NSDocumentationDirectory应该真的NSDocumentDirectory

文档目录在iOS上不存在,所以你不能在那里写。

你真的想在NSDocumentationDirectory目录下编写吗? 我认为,这个目录不是默认创build的(所以你必须先创build它)。

也许你想要NSDocumentDirectory 。 我试过了,你的代码工作。