簡體   English   中英

無法識別的選擇器已發送給班級(單人)

[英]Unrecognized selector sent to class (singleton)

我已經找到了與此問題相關的一些主題,但是我不明白那里提出的解決方案如何適用於單例課程。

當我的應用程序啟動時,我在applicationDidBecomeActive調用gamestate.shardInstance() ,但每次都會立即崩潰並出現相同的錯誤:

2017-03-22 11:09:09.102 project-mars[46108:28079112] +[project_mars.gameState encodeWithCoder:]: unrecognized selector sent to class 0x103670e60 2017-03-22 11:09:09.107 project-mars[46108:28079112] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '+[project_mars.gameState encodeWithCoder:]: unrecognized selector sent to class 0x103670e60'

有人可以幫助我了解此錯誤的確切原因以及如何解決該錯誤嗎? 謝謝!

class gameState: NSObject, NSCoding {

var GameSettings:gameSettings   //Structure that stores all the game settings
var PlayerStats:gameStatistics     //Structure that stores all game stats
var GameBoard:gameBoard        //Contains the information of the gameboard

private static var Instance: gameState = {
    let instance = gameState.loadgameState()
    //Closure for configuration
    return instance!
}()

private override init(){
    self.GameSettings = gameSettings()
    self.PlayerStats = gameStatistics()
    self.GameBoard = gameBoard()
    super.init()
}

class func sharedInstance() -> gameState{
    return gameState.Instance
}

required init?(coder aDecoder: NSCoder) {
    self.GameSettings = (aDecoder.decodeObject(forKey: "GameSettings") as? gameSettings)!
    self.PlayerStats = (aDecoder.decodeObject(forKey: "PlayerStats") as? gameStatistics)!
    self.GameBoard = (aDecoder.decodeObject(forKey: "GameBoard") as? gameBoard)!
}
func encode(with aCoder: NSCoder) {
    aCoder.encode(gameState.sharedInstance().GameSettings, forKey: "GameSettings")
    aCoder.encode(gameState.sharedInstance().PlayerStats, forKey: "PlayerStats")
    aCoder.encode(gameState.sharedInstance().GameBoard, forKey: "GameBoard")
}
class func loadgameState() ->gameState? {
    let path = gameState.getFilePath()
    print("PATH: \(path) \n")
    if gameState.fileExistsAtPath(path: path){
        if let rawData = NSData(contentsOfFile: path) {
            /// do we get serialized data back from the attempted path?
            /// if so, unarchive it into an AnyObject, and then convert to a GameData object
            if(rawData.length != 0){
                if let data = NSKeyedUnarchiver.unarchiveObject(with: rawData as Data) as? gameState {
                    return data
                }
            }
        }
        return gameState()
    }else{
        return gameState()
    }
}

我認為您應該能夠使用本地實例,而無需在此處調用單例。 您已經在單例實例中

func encode(with aCoder: NSCoder) {
    aCoder.encode(gameState.GameSettings, forKey: "GameSettings")
    aCoder.encode(gameState.PlayerStats, forKey: "PlayerStats")
    aCoder.encode(gameState.GameBoard, forKey: "GameBoard")
}

請共享gameState.loadgameState()靜態方法-我的猜測是那里正在發生其他事情。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM