Ios 无法调用';GameSetting.init';具有类型为的参数列表

Ios 无法调用';GameSetting.init';具有类型为的参数列表,ios,swift,sprite-kit,Ios,Swift,Sprite Kit,怎么了?我不理解这个错误 在这个问题上我找不到任何东西 我想完成保存和加载分数数据,下面是一个示例: 您尚未使用这些参数定义init方法。你可能想要这样的东西: import UIKit struct PropertyKeys { static let keyHightScore = "hightScore" static let keyLastScore = "lastScore" static let keyCurrentScore = "currentScore"

怎么了?我不理解这个错误

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

我想完成保存和加载分数数据,下面是一个示例:

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

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()
    }
}
或使用指定的参数创建便利初始化:

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
方法。你可能想要这样的东西:

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()
    }
}
或使用指定的参数创建便利初始化:

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
}

看起来您有一个放错位置的“}”,这会导致添加一个放错位置的
self

如果您有:

convenience init(highScore: Int, currentScore: Int, lastScore: Int) {
    self.init()
    self.hightScore = highScore
    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: Int, currentScore: Int, lastScore: Int) {
        self.hightScore = hightScore
        self.currentScore = currentScore
        self.lastScore = lastScore
    }
}
而且它看起来无论如何都不会让
init
失败,所以它没有理由失败,而且变量不会被更改,所以让它们
let

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
}

看起来您有一个放错位置的“}”,这会导致添加一个放错位置的
self

如果您有:

convenience init(highScore: Int, currentScore: Int, lastScore: Int) {
    self.init()
    self.hightScore = highScore
    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: Int, currentScore: Int, lastScore: Int) {
        self.hightScore = hightScore
        self.currentScore = currentScore
        self.lastScore = lastScore
    }
}
而且它看起来无论如何都不会让
init
失败,所以它没有理由失败,而且变量不会被更改,所以让它们
let

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
}

无法使用类型为(HightCore:Int.type,currentScore:Int.type,lastScore:Int.type,()->())的参数列表调用“GameSetting.init”,无法使用类型为(HightCore:Int.type,currentScore:Int.type,lastScore:Int.type,()->())的参数列表调用“GameSetting.init”