如何覆盖此Swift属性?

时间:2016-05-24 05:50:35

标签: ios swift

class Base: UIViewController {
    var rightButtonColor: UIColor = UIColor.blueColor()
}


class SecondViewController: Base {    
    override var rightButtonColor: UIColor {
        return UIColor.redColor() 
    }
}

我收到了一个错误:

  

使用Objective-C选择器获取'rightButtonColor'   'rightButtonColor'与来自'rightButtonColor'的getter冲突   超类'Base'具有相同的Objective-C   选择

2 个答案:

答案 0 :(得分:5)

试试这样:

class Base: UIViewController {
    var rightButtonColor: UIColor {
        return UIColor.blueColor()
    }
}

class SecondViewController: Base {
    override var rightButtonColor: UIColor {
        return UIColor.redColor()
    }
}

答案 1 :(得分:-2)

rightButtonColor的两个不同声明有不同的类型。如果你确定它们是完全相同的类型,它会干净地编译:

class Base: UIViewController {
    var rightButtonColor: UIColor {
        return UIColor.blueColor()
    }
}

class SecondViewController: Base {
    override var rightButtonColor: UIColor {
        return UIColor.redColor()
    }
}