Ios 写入值后获取plist数据时出错
我正在尝试创建一个plist文件,以便在我的Ios 写入值后获取plist数据时出错,ios,nsdictionary,swift,exc-bad-instruction,Ios,Nsdictionary,Swift,Exc Bad Instruction,我正在尝试创建一个plist文件,以便在我的SKScene和设置视图控制器之间共享数据。 由于我允许用户在游戏期间的任何时间访问设置,如果用户进行了任何更改,我总是从开始游戏的plist文件b4获取数据 func startGame() { gameStarted = true; plistPath = NSBundle.mainBundle().pathForResource("Settings", ofType: "plist"); dict = NSMutable
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(“设置”,类型为:“plist”)代码>总是抛出EXC\u BAD\u指令
错误
这就是我如何改变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);
编辑:我注意到问题来自这行让speed:CGFloat=dict.valueForKey(“speed”)作为CGFloat代码>删除后似乎不会抛出错误,但我需要它从plist读取值?尝试使用
var plistPath = NSBundle.mainBundle().pathForResource("Settings", ofType: "plist");
而不是
let plistPath = NSBundle.mainBundle().pathForResource("Settings", ofType: "plist");
这解决了我的问题