2015-04-24 68 views
0

此刻,我的數據不會在另一個新的會話中檢索到,並且它使用默認值。我必須有一個現有的plist文件才能工作嗎?我嘗試使用現有的文件沒有運氣。NSCoding gamedata not saved swift

我跟着這裏導向http://battleofbrothers.com/sirryan/saving-game-data-in-spritekit

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 = savedData 
     } 
     Static.instance = gamedata 
    } 

    return Static.instance! 
} 

override init() { 
    super.init() 
} 

required init(coder: NSCoder) { 
    super.init() 
} 

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

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 
     } 
    } 
    println("we returned Nil") 
    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); 
} 

}

我加載GAMEDATA在init函數與此

var gameData: GameData = GameData.sharedInstance 

更新數據

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

回答

0

像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); 
} 

}

0

保存數據,但不要在init中使用它。在initWithCoder

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