没有找到plist文件

时间:2013-04-05 15:06:04

标签: iphone ios cocoa-touch

我正在尝试将一个简单的数组写入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;
}

我把这个方法调用了两次,在第一次它不应该找到文件并且第一次写它。然后第二个调用应该能够找到该文件,但事实并非如此。谁能看到我在哪里错了?

2 个答案:

答案 0 :(得分:5)

同样,Xcode愚蠢的自动完成让你迷失:NSDocumentationDirectory应该是NSDocumentDirectory

iOS上不存在Documentation目录,因此您无法在那里写。

答案 1 :(得分:0)

你真的想写在NSDocumentationDirectory目录吗?我认为,默认情况下不会创建此目录(因此您必须在之前创建)。

也许你想要NSDocumentDirectory。我试过了,你的代码也运行了。

相关问题