Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Saving an array with GameData
class Level : NSObject, NSCoding {
var highScore : Double = 0
var completed : Bool = false
var revealed : Bool = false
required init?(coder: NSCoder) {
self.highScore = coder.decodeDouble(forKey: "highScore")
self.completed = coder.decodeBool(forKey: "completed")
self.revealed = coder.decodeBool(forKey: "revealed")
}
func encode(with coder: NSCoder) {
coder.encode(self.highScore, forKey: "highScore")
coder.encode(self.completed, forKey: "completed")
coder.encode(self.revealed, forKey: "revealed")
}
}
class LevelData : NSObject, NSCoding {
/// Some other portion of your code may populate the level defaults and change them as your progress.
/// This code is just proof of concept
var levels = Array<Level>()
override init() {
super.init()
}
required init?(coder: NSCoder) {
super.init()
let savedLevelData = coder.decodeObject(forKey: "levels") as! Array<Level>
for (key, data) in savedLevelData.enumerated() {
let actualLevel = self.levels[key]
actualLevel.highScore = data.highScore
actualLevel.completed = data.completed
actualLevel.revealed = data.revealed
}
}
func encode(with coder: NSCoder) {
coder.encode(self.levels, forKey: "levels")
}
}
Owner

veeneck commented Jul 1, 2016

And here is code using the GameData class: https://gist.github.com/veeneck/1d988af6e20e67b131c9cf775f546f16

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment