【问题标题】:writing to plist works on simulator but not on device [duplicate]写入plist适用于模拟器但不适用于设备[重复]
【发布时间】:2012-12-11 20:55:35
【问题描述】:

可能重复:
Not able to write to 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)。

任何帮助都意义重大!

【问题讨论】:

标签: ios plist


【解决方案1】:

应用程序包在模拟器上是可读写的,但在设备上是只读的。当您的应用首次启动时,您应该将 plist 复制到 Documents 目录(如果需要备份)或 Library/Caches(如果不需要),然后在您的应用中使用该版本。

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2012-11-01
  • 2011-03-08
  • 2015-02-13
  • 2011-03-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多