【发布时间】:2016-05-23 09:13:13
【问题描述】:
我有以下来自“ScoreHistory.swift”的变量:
// ScoreHistory.swift
var datePlayed: NSDate
var totalScore: Int
var totalAnswered: Int
var totalDuration: Int
var gameStatus: String
我正在尝试将数据显示到“ScoreHistoryViewController.swift”
// ScoreHistory.swift
navigationItem.title = ScoreHistory.datePlayed
datePlayedLabel.text = ScoreHistory.datePlayed
totalScoreLabel.text = ScoreHistory.totalScore
totalAnsweredLabel.text = ScoreHistory.totalAnswered
totalDurationLabel.text = ScoreHistory.totalDuration
gameStatusLabel.text = ScoreHistory.gameStatus
但他们都错了: 实例成员“____”不能用于“ScoreHistory”类型
这是什么意思?
我的 ScoreHistory.swift 的完整代码如下所示:
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。看起来您调用的是类的属性而不是实例。 -
@vadian 我已经用我的代码更新了我的问题