[英]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.