Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/100.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios NSCoding游戏数据未保存swift_Ios_Swift_Save_Plist_Nscoding - Fatal编程技术网

Ios NSCoding游戏数据未保存swift

Ios NSCoding游戏数据未保存swift,ios,swift,save,plist,nscoding,Ios,Swift,Save,Plist,Nscoding,目前,我的数据不会在另一个新会话中检索,而是使用默认值。我必须有一个现有的plist文件才能工作吗?我尝试使用现有的文件,但没有成功 我跟着导游来到这里 } 我用这个函数在init函数中加载gamedata var gameData: GameData = GameData.sharedInstance 更新数据 gameData.variableC = gameData.variableC + 1 gameData.save() println(gameData.va

目前,我的数据不会在另一个新会话中检索,而是使用默认值。我必须有一个现有的plist文件才能工作吗?我尝试使用现有的文件,但没有成功

我跟着导游来到这里

}

我用这个函数在init函数中加载gamedata

var gameData: GameData = GameData.sharedInstance
更新数据

    gameData.variableC = gameData.variableC + 1
    gameData.save()
    println(gameData.variableC)

您保存了数据,但不在init中使用它。在initWithCoder中:


就像Jozsef说的,我忘了解码数据。但是,我需要解码每个单独的变量,然后将其复制到GameData中,使其工作

这是

class GameData : NSObject, NSCoding {

/// Data to save

var variableC : Int! = 3

/// Create of shared instance

class var sharedInstance: GameData {

    struct Static {
        static var instance: GameData?
        static var token: dispatch_once_t = 0
    }

    dispatch_once(&Static.token) {
        var gamedata = GameData()
        if let savedData = GameData.loadGame() {
            gamedata.variableC = savedData.variableC
        }
        Static.instance = gamedata
    }

    return Static.instance!
}

override init() {
    super.init()
}

required init(coder: NSCoder) {
    super.init()
    self.variableC = coder.decodeObjectForKey("variableC") as? Int

}

func encodeWithCoder(coder: NSCoder) {
    coder.encodeObject(GameData.sharedInstance.variableC, forKey: "variableC")
}

class func loadGame() -> GameData? {
    // load existing high scores or set up an empty array
    let paths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)
    let documentsDirectory = paths[0] as! String
    let path = documentsDirectory.stringByAppendingPathComponent("GameData.plist")
    let fileManager = NSFileManager.defaultManager()

    // check if file exists
    if !fileManager.fileExistsAtPath(path) {
        // create an empty file if it doesn't exist
        println("File doesn't exist")
        if let bundle = NSBundle.mainBundle().pathForResource("DefaultFile", ofType: "plist") {
            fileManager.copyItemAtPath(bundle, toPath: path, error:nil)
        }
    }

    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 an array of HighScores, if possible
        if let data = NSKeyedUnarchiver.unarchiveObjectWithData(rawData) as? GameData {
            println("We loaded the data!")
            return data
        }
    }
    return nil
}

func save() {
    // find the save directory our app has permission to use, and save the serialized version of self.scores - the HighScores array.
    let saveData = NSKeyedArchiver.archivedDataWithRootObject(GameData.sharedInstance);
    let paths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true) as NSArray;
    let documentsDirectory = paths.objectAtIndex(0)as! NSString;
    let path = documentsDirectory.stringByAppendingPathComponent("GameData.plist");

    saveData.writeToFile(path, atomically: true);
}
}

required init(coder: NSCoder) {
        super.init()
        GameData.shaderInstance = coder.decodeObjectForKey("GameData")
    }
class GameData : NSObject, NSCoding {

/// Data to save

var variableC : Int! = 3

/// Create of shared instance

class var sharedInstance: GameData {

    struct Static {
        static var instance: GameData?
        static var token: dispatch_once_t = 0
    }

    dispatch_once(&Static.token) {
        var gamedata = GameData()
        if let savedData = GameData.loadGame() {
            gamedata.variableC = savedData.variableC
        }
        Static.instance = gamedata
    }

    return Static.instance!
}

override init() {
    super.init()
}

required init(coder: NSCoder) {
    super.init()
    self.variableC = coder.decodeObjectForKey("variableC") as? Int

}

func encodeWithCoder(coder: NSCoder) {
    coder.encodeObject(GameData.sharedInstance.variableC, forKey: "variableC")
}

class func loadGame() -> GameData? {
    // load existing high scores or set up an empty array
    let paths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)
    let documentsDirectory = paths[0] as! String
    let path = documentsDirectory.stringByAppendingPathComponent("GameData.plist")
    let fileManager = NSFileManager.defaultManager()

    // check if file exists
    if !fileManager.fileExistsAtPath(path) {
        // create an empty file if it doesn't exist
        println("File doesn't exist")
        if let bundle = NSBundle.mainBundle().pathForResource("DefaultFile", ofType: "plist") {
            fileManager.copyItemAtPath(bundle, toPath: path, error:nil)
        }
    }

    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 an array of HighScores, if possible
        if let data = NSKeyedUnarchiver.unarchiveObjectWithData(rawData) as? GameData {
            println("We loaded the data!")
            return data
        }
    }
    return nil
}

func save() {
    // find the save directory our app has permission to use, and save the serialized version of self.scores - the HighScores array.
    let saveData = NSKeyedArchiver.archivedDataWithRootObject(GameData.sharedInstance);
    let paths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true) as NSArray;
    let documentsDirectory = paths.objectAtIndex(0)as! NSString;
    let path = documentsDirectory.stringByAppendingPathComponent("GameData.plist");

    saveData.writeToFile(path, atomically: true);
}