-2

可能重复:
无法写入 Plist

我有这个问题。我有一个名为“instellingen”的 plist 文件。在该文件中,存储了不同的设置。例如,在启动时显示游戏规则。这在模拟器上工作得很好,但是当我在我的 iPad 上测试它时,plist dat 不会被保存。

这是我的代码:

- (void)viewDidLoad
{
    [super viewDidLoad];

    plistPath = [[NSBundle mainBundle] pathForResource:@"instellingen" ofType:@"plist"];
    instellingen = [NSMutableDictionary dictionaryWithContentsOfFile:plistPath];

    if ([[instellingen objectForKey:@"regelAtRuntime"] boolValue] == TRUE) {
        [self.showRulesButton setTitle:@"Spelregels niet meer tonen" forState:UIControlStateNormal];
    }else{
        [self.showRulesButton setTitle:@"Spelregels wel tonen" forState:UIControlStateNormal];
    }

    [self.mainContent flashScrollIndicators];

}

- (IBAction)dismissRules:(id)sender {
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (IBAction)dontShowRegels:(id)sender {

    if ([[instellingen objectForKey:@"regelAtRuntime"] boolValue] == TRUE) {
        [instellingen setValue:@"0" forKey:@"regelAtRuntime"];
    }else{
        [instellingen setValue:@"1" forKey:@"regelAtRuntime"];
    }

    [instellingen writeToFile:plistPath atomically:YES];
}

从可怕的 VC 那里读取规则似乎可以正常工作,使用这段代码:

NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"instellingen" ofType:@"plist"];
    instellingen = [NSDictionary dictionaryWithContentsOfFile:plistPath];

    if ([[instellingen objectForKey:@"regelAtRuntime"] boolValue] == TRUE) {
        NSLog(@"We're in!");
        [self performSegueWithIdentifier:@"showRulesSegue" sender:self];
    }

有谁知道我做错了什么?我试过运行一个干净的构建(Product Clean)。

任何帮助都意味着很多!

4

1 回答 1

1

The app bundle is read-write on Simulator but read-only on device. When your app is first launched, you should copy the plist to the Documents directory (if it needs to be backed up) or Library/Caches (if it doesn't), then use that version in your app.

于 2012-12-11T20:57:58.433 回答