简体   繁体   English

无法在 plist 文件中写入 Boolean

[英]Not Able to write Boolean in plist file

I am trying to write Boolean value to the plist file, But I really don't know where I'm doing wrong.我正在尝试将 Boolean 值写入 plist 文件,但我真的不知道我在哪里做错了。 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.此外文件路径也是正确的, myDict的分配表明已经从文件中读取了值。

Following is the code I have written以下是我写的代码

-(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]]; 

Edit:编辑:

If you need the plist from the main bundle you can copy it first in to the documents directory then modify it.如果您需要主包中的 plist,您可以先将其复制到文档目录中,然后对其进行修改。

    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);
    }

You cannot write/modify the files which are in your resource.您不能写入/修改资源中的文件。 they are only meant for reading.它们仅用于阅读。 Try creating the same(plist file) in the documnets directory of your application.尝试在应用程序的 documnets 目录中创建相同的(plist 文件)。

What the [myDict writeToFile:pathSettingFile atomically:NO]; [myDict writeToFile:pathSettingFile atomically:NO]; returns?回报? I think you can't write to your file.我认为您无法写入文件。

Seethis answer看到这个答案

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

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