Posts

Post not yet marked as solved
7 Replies
If you are using the legacy API, i.e., the ObservableObject protocol, why are you defining the object using the @State property wrapper? Instead, consider defining an object using the StateObject property wrapper. I recommend delving into materials on SwiftUI development before publishing questions or code. It will provide you with a better understanding of the concepts. 🙂🙂🙂
Post not yet marked as solved
6 Replies
Seems View and its members do not deallocate after a presentation Modal views usually have a separate hierarchy... So, I believe this should work: // Created by Dmitry Novosyolov on 31/10/2023. // import SwiftUI struct ContentView: View { @State private var someModalVM: SomeModalViewModel? = nil var body: some View { Button("present") { someModalVM = .init(modalValue: 10) } .fullScreenCover( item: $someModalVM, onDismiss: { print("Model Class value: \(someModalVM?.modalValue as Any)") } ) { ModalView(someModalVM: $0) } } } struct ModalView: View { @Environment(\.dismiss) private var dismiss let someModalVM: SomeModalViewModel var body: some View { Button("dismiss", action: dismiss.callAsFunction) .tint(.white) .frame(maxWidth: .infinity, maxHeight: .infinity) .background { Color.blue.ignoresSafeArea() } } } #Preview { ContentView() } @Observable final class SomeModalViewModel { let modalValue: Int init(modalValue: Int) { self.modalValue = modalValue print("Model Class value: \(self.modalValue)") } } extension SomeModalViewModel: Identifiable { }
Post not yet marked as solved
26 Replies
A very basic example - same behavior... '[SwiftUI] Publishing changes from within view updates is not allowed, this will cause undefined behavior.' import SwiftUI //MARK: Content View struct ContentView: View {     @StateObject     private var someVM: SomeViewModel = .shared     var body: some View {         VStack {             if let value = someVM.bindingValue {                 HStack {                     Text("value:".capitalized)                     Text(value.description)                 }                 .font(.largeTitle)             }             Button {                 someVM.isPresented.toggle()             } label: {                 Image(systemName: "exclamationmark.triangle.fill")                     .foregroundColor(.orange)                 Text("Hello, Beta 5 !")             }             .font(.title2)             .buttonStyle(.bordered)         }         .padding()         .sheet(isPresented: $someVM.isPresented) {             SheetView(someVM: .shared)         }     } } struct ContentView_Previews: PreviewProvider {     static var previews: some View {         ContentView()     } } //MARK: Sheet View struct SheetView: View {     @ObservedObject     var someVM: SomeViewModel     var body: some View {         NavigationStack {             List {                 ForEach(0..<10) { value in                     Button {                         someVM.updateValue(for: value)                     } label: {                         HStack {                             Text("value".capitalized)                             Spacer()                             Text(value.description)                                 .bold()                                 .foregroundColor(.red)                         }                     }                 }             }             .navigationTitle("Sheet View")         }     } } //MARK: View Model final class SomeViewModel: ObservableObject {     static let shared: SomeViewModel = .init()     @Published     private (set) var bindingValue: Int? = nil     @Published     var isPresented: Bool = false     func updateValue(for value: Int) {         Task { @MainActor in             bindingValue = value             isPresented.toggle()         }     } }
Post marked as solved
4 Replies
import SwiftUI struct MailFilterView: View {     @State     private var from = ""     @State     private var to = ""     @Binding     var filter: MailFilter     var body: some View {         NavigationView {             Form {                 Section(header: Text("from")) {                     TextField("from...", text: $from)                 }                 Section(header: Text("to")) {                     TextField("to...", text: $to)                 }                 Section(header: Text("save changes")) {                     Button(                         action: {                             filter.criteria.from = from                             filter.criteria.to = to                             // dismiss the view                         }, label: {                             Text("Save")                         })                         .disabled(from.isEmpty || to.isEmpty)                 }             }             .onAppear {                 from = filter.criteria.from                 to = filter.criteria.to             }         }     } } struct MailFilter {     var criteria: Criteria } struct Criteria {     var from, to: String }
Post not yet marked as solved
51 Replies
WWDC21 we are waiting for ...
Post not yet marked as solved
15 Replies
Obviously - this is a simulator problem- just run your code on a real device...
Post not yet marked as solved
11 Replies
I have the same issue:dumping backtrace: [x86_64] libnetcore-1872 0 libnetwork.dylib 0x00007fff500df068 __nw_create_backtrace_string + 120 1 libnetwork.dylib 0x00007fff5004149f nw_endpoint_get_type + 175 2 libboringssl.dylib 0x00007fff4ef09a6a nw_protocol_boringssl_get_subject_name + 178 3 libboringssl.dylib 0x00007fff4ef0c911 nw_protocol_boringssl_connected + 916 4 libnetwork.dylib 0x00007fff4fefcfdf nw_socket_handle_socket_event + 1711 5 libdispatch.dylib 0x000000010b62ed58 _dispatch_client_callout + 8 6 libdispatch.dylib 0x000000010b6316ca _dispatch_continuation_pop + 552 7 libdispatch.dylib 0x000000010b644a83 _dispatch_source_invoke + 2205 8 libdispatch.dylib 0x000000010b637815 _dispatch_workloop_invoke + 2631 9 libdispatch.dylib 0x000000010b641a4d _dispatch_workloop_worker_thread + 722 10 libsystem_pthread.dylib 0x00007fff511fd6d3 _pthread_wqthread.cold.1 + 125 11 libsystem_pthread.dylib 0x00007fff511f7856 _pthread_wqthread + 203 12 libsystem_pthread.dylib 0x00007fff511f7717 start_wqthread + 15