简体   繁体   English

无法读取/写入plist

[英]Unable to Read/write plist

I've looked into other users similar problems but I still can't find the problem with my code. 我已经调查了其他用户类似的问题,但是我的代码仍然找不到问题。

Here is the problem. 这是问题所在。

When the app start I call a method called setupDirectory that create (or copy from app bundle if needed) one plist for names and one for configs: 当应用启动时,我调用一个名为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;

}

Then when I need to add a name I call the method Add name to add to the plist the name: 然后,当我需要添加名称时,调用方法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];
}

But when I need to get the information from the plist I get nothing, so it didn't write? 但是,当我需要从plist中获取信息时,我什么也没得到,所以它没有写出来?

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

GetallNames return an empty array. GetallNames返回一个空数组。

Sorry if this question has already been answered, but I really looked for and still don't know what's going wrong 抱歉,如果您已经回答了这个问题,但我确实在寻找,仍然不知道出了什么问题

EDIT 1: 编辑1:

I used the return values of writeToFile to check if anything fail. 我使用writeToFile的返回值检查是否失败。 I'm getting a YES with the writeToFile from configsPlist, but a NO with writeToFile from NamesPlist. 我从configsPlist中的writeToFile得到是,但从NamesPlist中的writeToFile得到否。 So I think config is working fine, but not namesPlist 所以我认为配置工作正常,但不能使用namesPlist

EDIT 2: 编辑2:

Using the debugger I notice that ![fileManager fileExistsAtPath:configPath] and !fileManager fileExistsAtPath:dataPath] are return NO so I'm not actually getting into these if statments. 使用调试器,我注意到![fileManager fileExistsAtPath:configPath]!fileManager fileExistsAtPath:dataPath]返回NO因此如果声明,我实际上并没有参与其中。

Solution

Ok, I gave it another chance and tried to create with a string instead of the [NSNull null] and now it works... I thought I should use [NSNull null] to create a dictionary with null object 好的,我给了它另一个机会,并尝试使用字符串而不是[NSNull null]进行创建,现在它可以工作了...我想我应该使用[NSNull null]创建具有null对象的字典

I suggest to go to the documents directory : /Users/administrator/Library/Application Support/iPhone Simulator/YourApp/Documents/ and check the plist every time you add any item in it. 我建议转到文档目录:/ Users / administrator / Library / Application Support / iPhone Simulator / YourApp / Documents /,并在每次添加任何项目时检查plist。 With you can confirm if the "NamesPlist.plist" and "ConfigPlist" are getting updated or not. 可以确认“ NamesPlist.plist”和“ ConfigPlist”是否正在更新。

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

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