使用NSCoding一個視圖控制器產生的價值我有一個從「ScoreHistory.swift」以下變量:如何存儲在斯威夫特
// ScoreHistory.swift
class ScoreHistory: NSObject, NSCoding {
var datePlayed: NSDate
var totalScore: Int
var totalAnswered: Int
var totalDuration: Int
var gameStatus: String
在我QuizViewController,我有以下變量:
// QuizViewController.swift
var datePlayed = NSDate()
var totalScore = 0
var totalAnswered = 0
var totalDuration = 0
var gameStatus: String?
每場比賽後,用戶可能會得到以下內容:(將顯示在「ScoreViewController」中)
// Example data
datePlayed: (date today)
totalScore: 10
totalAnswered: 15
totalDuration: 1 min. 3 sec.
gameStatus: Exam Finished
我需要從QuizViewController得到上面的數據存儲到「ScoreHistory.swift」 我需要顯示在ScoreViewController.swift數據:
// ScoreViewController.swift
class ScoreViewController: UIViewController, UINavigationControllerDelegate {
// in viewDidLoad()
if let score = score {
navigationItem.title = score.datePlayed.description
datePlayedLabel.text = score.datePlayed.description
totalScoreLabel.text = score.totalScore.description
totalAnsweredLabel.text = score.totalAnswered.description
totalDurationLabel.text = score.totalDuration.description
gameStatusLabel.text = score.gameStatus
}
如何存放從QuizViewContoller到ScoreHistory生成的數據?以及如何將所述數據顯示給ScoreViewController?
我ScoreHistory看起來是這樣的:
class ScoreHistory: NSObject, NSCoding {
// MARK: Properties
var datePlayed: NSDate
var totalScore: Int
var totalAnswered: Int
var totalDuration: Int
var gameStatus: String
// MARK: Archiving Paths
static let DocumentsDirectory = NSFileManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask).first!
static let ArchiveURL = DocumentsDirectory.URLByAppendingPathComponent("scores")
// MARK: Types
struct PropertyKey {
static let datePlayedKey = "datePlayed"
static let totalScoreKey = "totalScore"
static let totalAnsweredKey = "totalAnswered"
static let totalDurationKey = "totalDuration"
static let gameStatusKey = "gameStatus"
}
// MARK: Initialization
init?(datePlayed: NSDate, totalScore: Int, totalAnswered: Int, totalDuration: Int, gameStatus: String) {
// Initialize stored properties.
self.datePlayed = datePlayed
self.totalScore = totalScore
self.totalAnswered = totalAnswered
self.totalDuration = totalDuration
self.gameStatus = gameStatus
super.init()
// Initialization should fail if there is no name or if the rating is negative.
if gameStatus.isEmpty {
return nil
}
}
// MARK: NSCoding
func encodeWithCoder(aCoder: NSCoder) {
aCoder.encodeObject(datePlayed, forKey: PropertyKey.datePlayedKey)
aCoder.encodeInteger(totalScore, forKey: PropertyKey.totalScoreKey)
aCoder.encodeInteger(totalAnswered, forKey: PropertyKey.totalAnsweredKey)
aCoder.encodeInteger(totalDuration, forKey: PropertyKey.totalDurationKey)
aCoder.encodeObject(gameStatus, forKey: PropertyKey.gameStatusKey)
}
required convenience init?(coder aDecoder: NSCoder) {
let datePlayed = aDecoder.decodeObjectForKey(PropertyKey.datePlayedKey) as? NSDate
let totalScore = aDecoder.decodeIntegerForKey(PropertyKey.totalScoreKey)
let totalAnswered = aDecoder.decodeIntegerForKey(PropertyKey.totalAnsweredKey)
let totalDuration = aDecoder.decodeIntegerForKey(PropertyKey.totalDurationKey)
let gameStatus = aDecoder.decodeObjectForKey(PropertyKey.gameStatusKey) as! String
// Must call designated initializer.
self.init(datePlayed: datePlayed!, totalScore: totalScore, totalAnswered: totalAnswered, totalDuration: totalDuration, gameStatus: gameStatus)
}
}
你會忽略你的類ScoreHistory的最重要的部分,你都應該實現NSCoding編碼器和解碼器 –
@LeoDabus,我更新了我的問題,請您檢查 – mjoe7
你可以開始改變你的文件的名稱scores.plist –