繁体   English   中英

无法读取/写入plist

[英]Unable to Read/write plist

我已经调查了其他用户类似的问题,但是我的代码仍然找不到问题。

这是问题所在。

当应用启动时,我调用一个名为setupDirectory的方法来创建(或从应用程序包复制,如果需要)一个plist作为名称,一个plist作为配置:

@implementation Data

static NSString * NamePlist;
static NSString * ConfigPlist;

+(void) setupDirectory
{

    //Names
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString * dataPath = [paths objectAtIndex:0];
    dataPath = [dataPath stringByAppendingPathComponent:@"NamesPlist.plist"];
    NSFileManager *fileManager = [NSFileManager defaultManager];

    if (![fileManager fileExistsAtPath:dataPath]) {
        NSString *sourcePath = [[NSBundle mainBundle] pathForResource:@"NamesPlist" ofType:@"plist"];
        [fileManager copyItemAtPath:sourcePath toPath:dataPath error:nil];
    }

    //Config
    NSString *configPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    configPath = [configPath stringByAppendingPathComponent:@"ConfigPlist.plist"];

    if (![fileManager fileExistsAtPath:configPath]) {
        NSString *sourcePath = [[NSBundle mainBundle] pathForResource:@"ConfigPlist" ofType:@"plist"];
        [fileManager copyItemAtPath:sourcePath toPath:configPath error:nil];
    }

NamePlist = dataPath;
ConfigPlist = configPath;

}

然后,当我需要添加名称时,调用方法Add name将名称添加到plist中:

+(void) addName: (NSString *) name andConfigs: (NSArray *) configs
{
    // Name

    // Get the old content of the plist file
    NSDictionary *oldContent = [NSDictionary dictionaryWithContentsOfFile:Namelist];
    // Copy the old content of the plist file to a new dictionary
    NSMutableDictionary * newContent = [oldContent mutableCopy];
    //Add the new config to the dictionary
    [newContent setObject:[NSNull null] forKey:name];
    // Set the new dictionary as the plist file
    [newContent writeToFile:NamePlist atomically:YES];

    //Configs

    oldContent = [NSDictionary dictionaryWithContentsOfFile:ConfigPlist];
    // Copy the old content of the plist file to a new dictonary
    newContent = [oldContent mutableCopy];
    //Add the new config to the dictionary
    [newContent setObject:configs forKey:name];
    // Set the new dictionary as the plist file
    [newContent writeToFile:ConfigPlist atomically:YES];
}

但是,当我需要从plist中获取信息时,我什么也没得到,所以它没有写出来?

+(NSArray *) getAllNames
{
    NSDictionary * names = [NSDictionary dictionaryWithContentsOfFile:NamePlist];
    NSArray * allNames = [(NSArray*) [names allKeys] sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];
    return allNames;
}

GetallNames返回一个空数组。

抱歉,如果您已经回答了这个问题,但我确实在寻找,仍然不知道出了什么问题

编辑1:

我使用writeToFile的返回值检查是否失败。 我从configsPlist中的writeToFile得到是,但从NamesPlist中的writeToFile得到否。 所以我认为配置工作正常,但不能使用namesPlist

编辑2:

使用调试器,我注意到![fileManager fileExistsAtPath:configPath]!fileManager fileExistsAtPath:dataPath]返回NO因此如果声明,我实际上并没有参与其中。

好的,我给了它另一个机会,并尝试使用字符串而不是[NSNull null]进行创建,现在它可以工作了...我想我应该使用[NSNull null]创建具有null对象的字典

我建议转到文档目录:/ Users / administrator / Library / Application Support / iPhone Simulator / YourApp / Documents /,并在每次添加任何项目时检查plist。 可以确认“ NamesPlist.plist”和“ ConfigPlist”是否正在更新。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM