https://kotlinlang.org logo
#multiplatform
Title
# multiplatform
p

Pavel Lahoda

03/14/2024, 1:53 PM
I run into interesting problem on iOS. I have a SwiftUI view:
struct MyView: View {
@State var path = NavigationPath()
body with NavigationStack....
func openDetail() {
path.append("mystring")
print("path: \(path.count)")
}
}
When I call openDetail() from my KMM code, the State var path seems not to be updated. Prints "path: 0" (and therefore the navigation doesn't work). The whole MyView is not getting reinitialized (init is not called) Any ideas?
p

Paweł Kulikowski

03/14/2024, 2:36 PM
It works if you call this code from ios? It’s hard to say what might be wrong with the stack not visible in the snippet
p

Pavel Lahoda

03/14/2024, 3:07 PM
Yep, it seems to work. I have a trivial NavigationStack whis is bound to the navigationPath, but I feel the core problem is in the fact @State is not updated - I append the element, and when I read from it on the next line, it is not there.
p

Paweł Kulikowski

03/14/2024, 5:28 PM
How do you call this method in KMM?
KMM integrates using Objective-C so I think passing a struct to any KMM code will result in a copy being made
but it depends how you integrate with KMM
p

Pavel Lahoda

03/14/2024, 5:31 PM
Thanks for tips, in the meantime I think I discovered the root cause, which lies in SwiftUI and has nothing to do with KMM. Still don't understand the details fully, but changing the @State to @ObservedObject did the trick for me.
p

Paweł Kulikowski

03/14/2024, 5:33 PM
👍
2 Views