无法调用GameSetting.init'使用类型的参数列表

时间:2016-03-18 19:48:55

标签: ios swift sprite-kit

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()
    }
}

有什么问题?我不明白这个错误。

我在这个问题上找不到任何东西。

我想完成保存并加载得分数据,我正在关注此示例: https://developer.apple.com/library/ios/referencelibrary/GettingStarted/DevelopiOSAppsSwift/Lesson10.html

2 个答案:

答案 0 :(得分:3)

您尚未使用这些参数定义init方法。你可能想要这样的东西:

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
}

或使用指定的参数创建一个便利init:

convenience init(highScore: Int, currentScore: Int, lastScore: Int) {
    self.init()
    self.hightScore = highScore
    self.currentScore = currentScore
    self.lastScore = lastScore
}

答案 1 :(得分:1)

看起来你有一个错位'}'会导致添加错误的self

你在哪里:

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
    }
}

我想你想要:

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
}

并且看起来好像init失败了,所以没有理由让它变得可用,并且变量不会被改变,所以让它们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)
}