我做了一个 UILabel
的子类,叫做 UIScoreLabel
,它更适合我显示分数的需要。这是代码:
class UIScoreLabel: UILabel
{
var scoreBackingInt: Int!
var score: Int {
get
{
return scoreBackingInt
}
set(newScore)
{
scoreBackingInt = newScore
self.text = NSString(format: "%0\(digits)d", newScore)
}
}
let digits: Int! // number of digits to display (for 0-padding)
init(digits: Int)
{
super.init()
self.digits = digits
self.score = 0
}
}
score
应该是一个计算变量。无论如何,当我实例化一个 UIScoreLabel 时一切都很好,但是当我试图以任何方式访问任何子类的属性时(即 score
),编译器告诉我 'UILabel' 不有一个名为“score”的成员
这是在我的 ViewController 中给我一个错误的行:
creditsLabel.score = self.score
什么给了?
最佳答案
你需要让编译器知道类的类型。正如您在错误消息中看到的那样,编译器当前认为该类是 UILable
,因此您需要更新类类型(如果不能,则强制转换类类型)。
关于ios - 为什么我的子类变量不存在?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27056184/