[英]Not Able to write Boolean in plist file
我正在尝试将 Boolean 值写入 plist 文件,但我真的不知道我在哪里做错了。 我什至没有收到任何异常/错误,但相关文件中的值保持不变。 此外文件路径也是正确的, myDict
的分配表明已经从文件中读取了值。
以下是我写的代码
-(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];
}
您不能写入主包。 您只能从主包中读取。 如果你想写一个文件,你需要把它放在你的应用程序的文档目录中。
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.plist",plistName]];
编辑:
如果您需要主包中的 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);
}
您不能写入/修改资源中的文件。 它们仅用于阅读。 尝试在应用程序的 documnets 目录中创建相同的(plist 文件)。
[myDict writeToFile:pathSettingFile atomically:NO];
回报? 我认为您无法写入文件。
看到这个答案
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.