0

I'd like to override descendant property, because I want to use methods from TestViewModel class directly without casting ViewModel to TestViewModel. As I suppose it's not possible. Could you advise some nice solution. Code and error below:

class ViewModel {
}

class View: UIView {
    weak var viewModel: ViewModel!
}

class TestViewModel {
}

class TestView: View {
    weak var viewModel: TestViewModel! // error here
}

Got this error:

/Users/evgeniirtishchev/Documents/Development/rdfuturesales/RDProject/View/Identification/IdentificationView.swift:12:26: Cannot override mutable property 'viewModel' of type 'ViewModel!' with covariant type 'IdentificationViewModel!'

katleta3000
  • 2,484
  • 1
  • 18
  • 23

1 Answers1

3

Swift does not allow you to change the class type of any variables or properties.

See related question: Overriding superclass property with different type in Swift

Community
  • 1
  • 1
s1ddok
  • 4,615
  • 1
  • 18
  • 31