【问题标题】:Instance member ‘____’ cannot be used on type ‘ScoreHistory’实例成员“____”不能用于“ScoreHistory”类型
【发布时间】: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 我已经用我的代码更新了我的问题

标签: ios swift


【解决方案1】:

根据您的模型设计,您无法访问类上的属性,例如 ScoreHistory.datePlayed

你必须创建一个实例。

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

并调用一个属性

navigationItem.title = history.datePlayed

【讨论】:

  • 我把你给我的实例代码放在viewDidLoad() 中,但是当我调用属性navigationItem.title = history.datePlayed 时,它给出了错误'不能将'NSDate 类型的值赋给类型'字符串?'
  • 因为navigationItemtitle 属性是String 类型而不是NSDate。从日期获取字符串的最简单方法是调用其description 属性:history.datePlayed.description
猜你喜欢
  • 2015-11-27
  • 2016-01-01
  • 2016-09-09
  • 2020-01-16
  • 2017-06-25
  • 2016-07-05
  • 2017-01-25
  • 2019-02-15
相关资源
最近更新 更多