找不到plist文件 [英] plist file not being found

查看:138
本文介绍了找不到plist文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图写一个简单的数组到plist文件,然后再检索它。我有以下代码:

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?

推荐答案

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

Again, Xcode's stupid autocomplete made you lost: NSDocumentationDirectory should really be NSDocumentDirectory.

Documentation目录不存在iOS,所以你不能写在那里。

The Documentation directory doesn't exist on iOS, so you can't write there.

这篇关于找不到plist文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆