【问题标题】:Error when getting plist data after writing value to it写入值后获取 plist 数据时出错
【发布时间】:2014-06-27 08:06:16
【问题描述】:

我正在尝试制作一个 plist 文件以在我的 SKScene 和设置视图控制器之间共享数据。 由于我允许用户在游戏过程中随时访问设置,因此如果用户进行任何更改,我总是从 plist 文件 b4 中获取数据以启动游戏。

func startGame() {
    gameStarted = true;

    plistPath = NSBundle.mainBundle().pathForResource("Settings", ofType: "plist");
    dict = NSMutableDictionary(contentsOfFile: plistPath);

    let speed: CGFloat = dict.valueForKey("Speed") as CGFloat;

}

如果用户根本不去设置更改设置,我可以运行此功能而不会中断和错误。
但是一旦我在设置视图控制器中更改了速度值,并且一旦调用了这个函数,plistPath = NSBundle.mainBundle().pathForResource("Settings", ofType: "plist"); 总是会抛出一个EXC_BAD_INSTRUCTION 错误。

这就是我从 plist 更改值的方式

let plistPath = NSBundle.mainBundle().pathForResource("Settings", ofType: "plist");
var dict: NSMutableDictionary = NSMutableDictionary(contentsOfFile: plistPath);

dict = NSMutableDictionary(dict.setValue(speed, forKey: "Speed"));
dict.writeToFile(plistPath, atomically: true);

编辑:我注意到问题来自这一行 let speed: CGFloat = dict.valueForKey("Speed") as CGFloat; 似乎一旦我删除它就不会引发任何错误,但是我需要它从 plist 中读取值?

【问题讨论】:

    标签: ios nsdictionary swift exc-bad-instruction


    【解决方案1】:

    尝试使用

    var plistPath = NSBundle.mainBundle().pathForResource("Settings", ofType: "plist");
    

    代替

    let plistPath = NSBundle.mainBundle().pathForResource("Settings", ofType: "plist");
    

    这解决了我的问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-17
      • 1970-01-01
      • 1970-01-01
      • 2012-07-14
      相关资源
      最近更新 更多