Reputation: 491
I have the following variables from "ScoreHistory.swift":
// ScoreHistory.swift
var datePlayed: NSDate
var totalScore: Int
var totalAnswered: Int
var totalDuration: Int
var gameStatus: String
I am trying to display the data to "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
but they are all in error: Instance member ‘____’ cannot be used on type ‘ScoreHistory’
What does that mean?
My ScoreHistory.swift's full code looks like this:
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)
}
Upvotes: 0
Views: 916
Reputation: 285290
According to your model design you cannot access a property on the class like ScoreHistory.datePlayed
You have to create an instance.
let history = ScoreHistory(datePlayed: NSDate(), totalScore: 0, totalAnswered: 0, totalDuration: 0, gameStatus: "started")
and call a property
navigationItem.title = history.datePlayed
Upvotes: 0