Hi all, I’d like to use a pattern where I have an ...
# kotlin-native
u
Hi all, I’d like to use a pattern where I have an abstract class
A
with an abstract property (
a
) and a class
B
which overrides property
a
. Now
A
lives in the K/N world and I want
B
to be
@Published
. I suppose there is no way to express the
@Published
annotation in K/N so
B
must be written in Swift. Now Swift does not know about abstract classes and thinks
a
was a stored property and complains:
Copy code
Cannot override with a stored property 'counter'
Do you guys see any way to get a subclass of Kotlin cass
A
with
@published
property
a
?
r
Does
B
need to subclass
A
instead of just containing an
A
?
u
A is my view model, shared across platforms. It contains abstract propeties to hold the view state. On each platform i inherit A and provide a reactive implementation of the state properties so that methods in A can just set values. Actually I first had an implementation where A was having a state object which was passed in its constructor. The state object, being of interface type could be implemented with @published properties. Now I changed my architecture in a way that the view creates and holds the viewmodel and it’s state object and found out that I can’t construct both, passing one to the other due to struct/immutability issues. That’s why I am trying with inheritance now.
This was my original approach:
Copy code
class ViewState: CounterViewState, ObservableObject {
    @Published var counter = "Loading ..."
}

struct CounterView: View {
    @StateObject var viewState: ViewState
    var viewModel: CounterViewModel

    init() {
        let state = ViewState()
        viewState = state // ##### Won't let me assign here
        viewModel = CounterViewModel(viewState: state)
    }
So I figured I need a single constructor, tried inheritance, moved state properties from state object right into the viewmodel and ran into the abstract property issue.
m
This is from memory so I might be wrong but I think that in Swift the Kotlin native classes with abstract functions could be overridden but without the override modifier. You just declare 'a' and it's overriding the abstract one. Or maybe it was about open functions