gpt4 book ai didi

ios - 实例成员 ‘____’ 不能用于类型 ‘ScoreHistory’

转载 作者:行者123 更新时间:2023-11-30 13:42:52 26 4
gpt4 key购买 nike

我有来自“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”上使用

这是什么意思?

My 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.datePlayed

您必须创建一个实例。

let history = ScoreHistory(datePlayed: NSDate(), totalScore: 0, totalAnswered: 0, totalDuration: 0, gameStatus: "started")

并调用属性

navigationItem.title = history.datePlayed

关于ios - 实例成员 ‘____’ 不能用于类型 ‘ScoreHistory’,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35358899/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com