Even though Apple migration guide suggests to use
@State I noticed this behavior too as I'm used to have a sort of MVVM in SwiftUI.
So I tried a mixed approach; I used a sort of wrapper which is both Observable and a @StateObject just for the sake of the lifetime, no @Published var within it.
@Observable
public final class Store<VM: Observable & AnyObject>: ObservableObject {
private var _viewModel: VM
public var viewModel: VM {
get { _viewModel }
set {} // set ignored
}
deinit {
print("Deinit", self)
}
init(_ viewModel: @escaping () -> VM) {
_viewModel = viewModel()
}
}
import SwiftUI
extension StateObject {
public init<ViewModel: Observable & AnyObject>(
viewModel: @escaping @autoclosure () -> ViewModel
) where ObjectType == Store<ViewModel> {
self.init(wrappedValue: .init(viewModel))
}
}
Then in the view
@StateObject private var store: Store<MyViewModel>
init(someInitialValue: String) {
_store = .init(viewModel: .init(value: someInitialValue))
}
var body: some View {
Text(store.viewModel.value)
TextField(text: $store.viewModel.editableText)
}
Did you find any other solution?
Post
Replies
Boosts
Views
Activity
After months of fighting with permission on Xcode Cloud, by chance I found what was the issue on my case!
In my plugin implementation I write the file through
"""
// auto generated swift file
// ... the content
""".write(to: output, atomically: true, encoding: .utf8)
Using atomically: true is the issue: this means that swift writes the entire content into a temporary file placed somewhere else and Xcode Cloud doesn't have permission in this moment!
Using atomically: false the file is written directly into our output file, which is the context.pluginWorkDirectory.appending("GeneratedImageAssets.swift") and permissions here hare granted!!
Anybody found a solution to have both previews and device/simulator? It's a bit frustrating not to have both