class ArcaneCardVC: UIViewController {
var currentCard: ArcaneCardView?
}
class PostVC: ArcaneCardVC {
override var currentCard: PostCard?
// <===== This is what I want to do but cant
}
class ArcaneCardView: UIView {
}
class PostCard: ArcaneCardView {
}
Here is the error I get:
Cannot override mutable property 'currentCard' of type 'ArcaneCardView?' with covariant type 'PostCard?'
The other solution is explicitly doing this in code everytime I use currentCard:
var card = currentCard as! PostCard
Best Answer
When you override a variable, you can't change it's type. Why? Suppose that you are able to do that, then the following scenario would be possible:
What should be the type of
B.currentCard
? Hmm, this is a complicated question. You can answer that its type should bePostCard
. Ok, lets add other classes to the party:Considerer now the following code:
For avoiding this kind of behavior, you can't change the type of a property when you are subclassing.