ios - 为什么我的子类变量不存在?

标签 ios swift

我做了一个 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/

相关文章:

swift - Swift 3 中 NSFetchRequests 的集合

iOS本地同时通知但不同日期有不同的提醒消息

ios - 在 AlamofireObjectMapper 响应中映射 allHeaderFields

swift - 使用 SWIFT 4 decoable 时出现未知上下文错误

ios - NEHotspotHelper - 可以在没有权利的情况下进行测试吗?

ios - Apple 在核心数据的上下文中将什么称为对象图?

ios - 避免 UIImage 的 imageNamed - 内存管理

ios - 设置 Sub-UIViewController 框架高度

ios - 我们如何创建像复选标记一样的 IBInspectable?

ios - 将 parse 中的数据保存为字符串