无法在plist文件中写入布尔值 [英] Not Able to write Boolean in plist file

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

问题描述

我正在尝试将布尔值写入plist文件,但我真的不知道我在哪里做错了。我甚至没有得到任何异常/错误,但相关文件中的值保持不变。此外,文件路径也是正确的, myDict 的分配显示已从文件中读取值。

I am trying to write Boolean value to the plist file, But I really don't know where I'm doing wrong. I am not even getting any Exception/Error yet the values remain unchanged in the concerned file. Furthermore file path is correct too, the allocation of myDict shows that the values ve been read from the file.

以下是我写的代码

-(void)SetSettings:(BOOL)SoundOn: (BOOL)GlowOn
{
    NSString *pathSettingFile = [[NSString alloc]initWithString:[[NSBundle mainBundle]pathForResource:@"AppSettings" ofType:@"plist"]];

    NSMutableDictionary *myDict = [NSMutableDictionary dictionaryWithContentsOfFile:pathSettingFile];

    [myDict setObject:[NSNumber numberWithBool:SoundOn] forKey:@"isSoundOn"];
    [myDict setObject:[NSNumber numberWithBool:GlowOn] forKey:@"isGlowOn"];
    [myDict writeToFile:pathSettingFile atomically:NO];

    [myDict release];
    [pathSettingFile release];
}


推荐答案

你不能写入主要包。您只能从主包中读取。如果要编写文件,则需要将其放入应用程序的文档目录中。

You can not write in to main bundle. You only can read from main bundle. If you want to write an file you need to place it in to the documents directory of your app.

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *path = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.plist",plistName]]; 

编辑:

如果您需要从主包中删除你可以先将它复制到文档目录然后修改它。

If you need the plist from the main bundle you can copy it first in to the documents directory then modify it.

    NSError *error;
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *path = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.plist",plistName]]; 

    NSFileManager *fileManager = [NSFileManager defaultManager];

    if (![fileManager fileExistsAtPath: path]){
        NSLog(@"File don't exists at path %@", path);

        NSString *plistPathBundle = [[NSBundle mainBundle] pathForResource:plistName ofType:@"plist"];

        [fileManager copyItemAtPath:plistPathBundle toPath: path error:&error]; 
    }else{
        NSLog(@"File exists at path:%@", path);
    }

这篇关于无法在plist文件中写入布尔值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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