Reputation: 33
import UIKit
struct PropertyKeys {
static let keyHightScore = "hightScore"
static let keyLastScore = "lastScore"
static let keyCurrentScore = "currentScore"
}
class GameSetting: NSObject, NSCoding {
var hightScore: Int
var currentScore: Int
var lastScore: Int
var life = 3
override init() {
hightScore = 0
currentScore = 0
lastScore = 0
super.init()
loadGameSetting()
}
func encodeWithCoder(aCoder: NSCoder) {
aCoder.encodeInteger(hightScore, forKey: PropertyKeys.keyHightScore)
aCoder.encodeInteger(currentScore, forKey: PropertyKeys.keyCurrentScore)
aCoder.encodeInteger(lastScore, forKey: PropertyKeys.keyLastScore)
}
required convenience init?(coder aDecoder: NSCoder) {
var hightScore = aDecoder.decodeIntegerForKey(PropertyKeys.keyHightScore)
var currentScore = aDecoder.decodeIntegerForKey(PropertyKeys.keyCurrentScore)
var lastScore = aDecoder.decodeIntegerForKey(PropertyKeys.keyLastScore)
self.init(hightScore: Int, currentScore: Int, lastScore: Int) {
self.hightScore = hightScore
self.currentScore = currentScore
self.lastScore = lastScore
}
}
func recordScore(score: Int) {
if score > hightScore { hightScore = score }
lastScore = score
saveGameSetting()
}
func saveGameSetting() {
NSUserDefaults.standardUserDefaults().setInteger(hightScore, forKey: PropertyKeys.keyHightScore)
NSUserDefaults.standardUserDefaults().setInteger(lastScore, forKey: PropertyKeys.keyLastScore)
}
func loadGameSetting() {
hightScore = NSUserDefaults.standardUserDefaults().integerForKey(PropertyKeys.keyHightScore)
lastScore = NSUserDefaults.standardUserDefaults().integerForKey(PropertyKeys.keyLastScore)
}
override var description: String {
return "HighScore: \(self.hightScore), lastScore: \(self.lastScore), currenScore: \(self.currentScore)"
}
func reset() {
currentScore = 0
}
func resetHightScore() {
hightScore = 0
lastScore = 0
saveGameSetting()
}
}
What is wrong? I don't understand this error.
I can not find anything on this issue.
I want to finished save and load score data, and I am following this example: https://developer.apple.com/library/ios/referencelibrary/GettingStarted/DevelopiOSAppsSwift/Lesson10.html
Upvotes: 1
Views: 1234
Reputation: 41226
It looks like you've got a misplaced '}' which lead to adding a misplaced self
Where you have:
required convenience init?(coder aDecoder: NSCoder) {
var hightScore = aDecoder.decodeIntegerForKey(PropertyKeys.keyHightScore)
var currentScore = aDecoder.decodeIntegerForKey(PropertyKeys.keyCurrentScore)
var lastScore = aDecoder.decodeIntegerForKey(PropertyKeys.keyLastScore)
self.init(hightScore: Int, currentScore: Int, lastScore: Int) {
self.hightScore = hightScore
self.currentScore = currentScore
self.lastScore = lastScore
}
}
I think you want:
required convenience init?(coder aDecoder: NSCoder) {
var hightScore = aDecoder.decodeIntegerForKey(PropertyKeys.keyHightScore)
var currentScore = aDecoder.decodeIntegerForKey(PropertyKeys.keyCurrentScore)
var lastScore = aDecoder.decodeIntegerForKey(PropertyKeys.keyLastScore)
self.init(hightScore:hightScore, currentScore:currentScore, lastScore:lastScore)
}
init(hightScore: Int, currentScore: Int, lastScore: Int) {
self.hightScore = hightScore
self.currentScore = currentScore
self.lastScore = lastScore
}
and it doesn't look like there's anyway for the init
to fail, so there's no reason for it to be failable, and the vars don't get changed, so make them let
:
required convenience init(coder aDecoder: NSCoder) {
let hightScore = aDecoder.decodeIntegerForKey(PropertyKeys.keyHightScore)
let currentScore = aDecoder.decodeIntegerForKey(PropertyKeys.keyCurrentScore)
let lastScore = aDecoder.decodeIntegerForKey(PropertyKeys.keyLastScore)
self.init(hightScore:hightScore, currentScore:currentScore, lastScore:lastScore)
}
Upvotes: 1
Reputation: 42449
You haven't defined an init
method with those arguments. You probably want something like this:
required convenience init?(coder aDecoder: NSCoder) {
let hightScore = aDecoder.decodeIntegerForKey(PropertyKeys.keyHightScore)
let currentScore = aDecoder.decodeIntegerForKey(PropertyKeys.keyCurrentScore)
let lastScore = aDecoder.decodeIntegerForKey(PropertyKeys.keyLastScore)
self.init()
self.hightScore = hightScore
self.currentScore = currentScore
self.lastScore = lastScore
}
or create a convenience init with the specified arguments:
convenience init(highScore: Int, currentScore: Int, lastScore: Int) {
self.init()
self.hightScore = highScore
self.currentScore = currentScore
self.lastScore = lastScore
}
Upvotes: 3