Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I override this Swift property?

Tags:

ios

swift

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


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

I'm getting an error:

Getter for 'rightButtonColor' with Objective-C selector 'rightButtonColor' conflicts with getter for 'rightButtonColor' from superclass 'Base' with the same Objective-C selector

like image 697
TIMEX Avatar asked May 24 '16 05:05

TIMEX


People also ask

Can we override properties in Swift?

Classes in Swift can call and access methods, properties, and subscripts belonging to their superclass and can provide their own overriding versions of those methods, properties, and subscripts to refine or modify their behavior.

What does override mean in Swift?

Swift version: 5.6. The override is used when you want to write your own method to replace an existing one in a parent class. It's used commonly when you're working with UIViewControllers , because view controllers already come with lots of methods like viewDidLoad() and viewWillAppear() .

How do I override a Swift extension?

An extension can't be overridden because the way Swift implement extension is using static dispatch which means its resolved at compile time.


1 Answers

Try like this:

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

class SecondViewController: Base {
    override var rightButtonColor: UIColor {
        return UIColor.redColor()
    }
}
like image 73
Pushpa Y Avatar answered Nov 01 '22 21:11

Pushpa Y