2016-02-16 60 views
0

我得到Score.swift和ScoreManager.swift。如何使用NSCoding保存數據

我Score.swift看起來是這樣的:

class Score: NSObject, NSCoding { 

let score:Int; 
let dateOfScore:NSDate; 

init(score:Int, dateOfScore:NSDate) { 
    self.score = score; 
    self.dateOfScore = dateOfScore; 
} 

required init(coder: NSCoder) { 
    self.score = coder.decodeObjectForKey("score") as! Int; 
    self.dateOfScore = coder.decodeObjectForKey("dateOfScore") as! NSDate; 
    super.init() 
} 

func encodeWithCoder(coder: NSCoder) { 
    coder.encodeObject(self.score, forKey: "score") 
    coder.encodeObject(self.dateOfScore, forKey: "dateOfScore") 
} 
} 

我ScoreManager.swift看起來是這樣的:

class ScoreManager { 
var scores:Array<Score> = []; 

init() { 
    // load existing high scores or set up an empty array 
    let documentsPath = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0] as NSString 
    let path = documentsPath.stringByAppendingPathComponent("Scores.plist") 
    let fileManager = NSFileManager.defaultManager() 

    // check if file exists 
    if !fileManager.fileExistsAtPath(path) { 
     // create an empty file if it doesn't exist 
     if let bundle = NSBundle.mainBundle().pathForResource("Scores", ofType: "plist") { 
      do { 
       try fileManager.copyItemAtPath(bundle, toPath: path) 
      } catch { 

      } 
     } 
    } 

    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 Scores, if possible 
     let scoreArray: AnyObject? = NSKeyedUnarchiver.unarchiveObjectWithData(rawData); 
     self.scores = scoreArray as? [Score] ?? []; 
    } 
} 

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

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

// a simple function to add a new high score, to be called from your game logic 
// note that this doesn't sort or filter the scores in any way 

func addNewScore(newScore:Int) { 
    let newScore = Score(score: newScore, dateOfScore: NSDate()); 
    self.scores.append(newScore); 
    self.save(); 
} 
} 

我的問題是這樣的: 如何把這些東西NSCoding保存數據從實際的gameView場景?

+0

你的編碼和解碼函數有個錯誤:不要在'score'上使用'encodeObject'。使用'encodeInteger'和'decodeInteger'。除此之外,你的'save'功能已經處理寫入文件。從您的角度調用它。 –

+0

謝謝,我如何從我的觀點調用它? – mjoe7

+0

將'ScoreManager'的實例添加到您的視圖控制器並從那裏調用 –

回答

0

我強烈建議您閱讀一本書或至少一本關於iOS編程的教程。但是這是最低的。

class ViewController: UIViewController { 
    let scoreManager = ScoreManager() 

    // Save the score to file. Hook it up to a button or label in your view 
    @IBAction func save (sender: AnyObject) { 
     scoreManager.save() 
    } 
} 

如果你不知道如何將一個按鈕/標籤連接到@IBAction,請找上谷歌的教程。