junyi00
junyi00

Reputation: 792

Error when getting plist data after writing value to it

I am trying to make a plist file to share data between my SKScene and settings view controller. Since I allow the user to access the settings any time during the game, if the user made any changes, I always get data from the plist file b4 starting the game.

func startGame() {
    gameStarted = true;

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

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

}

If the user does not go to the settings to change the settings at all, I can run this function with no interruptions and errors.
But once I change the speed value in the settings view controller, and once this function is called, plistPath = NSBundle.mainBundle().pathForResource("Settings", ofType: "plist"); always throws a EXC_BAD_INSTRUCTION error.

And this is how I change the value from the 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);

EDIT: I noticed the problem comes from this line let speed: CGFloat = dict.valueForKey("Speed") as CGFloat; It seems that once I remove it no errors are thrown, but its a I need it to read value from the plist?

Upvotes: 2

Views: 364

Answers (1)

Heena
Heena

Reputation: 2358

Try using

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

Instead of

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

this resolved my problem.

Upvotes: 1

Related Questions