Questions tagged [observedobject]

@ObservedObject is a SwiftUI property wrapper used to mark properties that are not owned by the View, but should update the View. Only ObservableObject conformant types can be annotated with the @ObservedObject property wrapper.

For more info, see the official documentation of @ObservedObject.

99 questions
6
votes
1 answer

What is the difference between @EnvironmentObject and @ObservedObject?

I have been reading about the property wrappers in SwiftUI and I see that they do a great job, but one thing which I really don't get is the difference between @EnvironmentObject and @ObservedObject. From what I learned so far, I see that…
Dakata
  • 914
  • 2
  • 8
  • 22
4
votes
2 answers

SwiftUI: How can I catch changing value from observed object when I execute function

I have a problem with observed object in SwiftUI. I can see changing values of observed object on the View struct. However in class or function, even if I change text value of TextField(which is observable object) but "self.codeTwo.text still did…
alice_ix
  • 89
  • 1
  • 7
4
votes
1 answer

What is the difference between @State and @ObservedObject, can they both be used to persist state?

When I Googled "State vs ObservedObject" the first result was from Hacking with Swift and it said about @ObservedObject: This is very similar to @State except now we’re using an external reference type rather than a simple local property like a…
Gil Birman
  • 32,601
  • 12
  • 66
  • 107
3
votes
1 answer

Updating the UI for a tree structure in SwiftUI

I have an ObservableObject class, Model0 which has a Published object nested. The nested object represents a recursive tree structure. I want to be able to update the tree structure and have the UI update appropriately. However, in the below code,…
BenJacob
  • 827
  • 8
  • 25
3
votes
1 answer

Passing an @ObservableObject to a nested view

I'm quite new to SwiftUI and trying to pass an array in a forEach-loop to another view, but Xcode says, that my 'Property type does not match that of the 'wrappedValue' property of its wrapper type 'ObservedObject'. I also found some similar…
gavisio
  • 55
  • 4
3
votes
3 answers

Change on ObservableObject in @Published array does not update the view

I've been struggling for hours on an issue with SwiftUI. Here is a simplified example of my issue : class Parent: ObservableObject { @Published var children = [Child()] } class Child: ObservableObject { @Published var name: String? …
user2742293
  • 39
  • 1
  • 4
3
votes
1 answer

SwiftUI List freezes on @ObservedObject update

I have the List which automatically fetches more data near the end: struct AdCardListView: View { @ObservedObject var model: AdListViewModel = AdListViewModel() var body: some View { List {…
3
votes
2 answers

How to trigger an action as soon as a SwiftUI observed variable changes its value

I've got a List in my View where its elements are going to be updated as soon as the list argument will change. This is my View: struct ContentView: View { @ObservedObject var users = Utenti() @State private var isSharePresented: Bool = false var…
2
votes
2 answers

SwiftUI observed object do action when its property is change

I have observed object which is created in view and I want to have function that will occur when the object bool property is changed. I want something similar to .onTapGesture. Is there a way to do it? have function where the body of the function is…
K. Ondřej
  • 45
  • 6
2
votes
3 answers

What is the difference between @StateObject and @ObservedObject in child views in swiftUI

I created a Model like this: class TestModel: ObservableObject { @Published var num: Int = 0 } Model is be used in "Home" view and "Home"s child view "HomeSub" struct Home: View { @StateObject var model = TestModel() var body:…
张黒猫
  • 23
  • 3
2
votes
2 answers

How to be notified of change to ObservedObject which conforms to a protocol

I'm building an AutoCompletion view and would like to pass an object which contains the field to be autocompleted. Currently I have two different types which I need to autocomplete on an attribute of, both attributes are named the same. I've created…
Houdi
  • 126
  • 9
2
votes
2 answers

SwiftUI ObservableObject not updating when value is Published

I have a view and a viewModel that should update the ListView when users are added to the user array. I can verify that users are being added, yet the ObservedObject is not updating. I have a search bar that lets you search users and then updates…
connorvo
  • 511
  • 5
  • 14
2
votes
2 answers

SwiftUI @Published and @ObservedObject not going to NavigationLink destination

Sorry for the title and length I struggled to summerise I have a class of @ObserableObject @Published data which I want to change and use the new data on a second page accessed via NavigationLink class Data: ObservableObject { @Published var…
2
votes
0 answers

SwiftUI Is it possible to use Toggle to update value of ObservableObject

I have a date selector in a view and, once the user enters a date and saves I display a new view with a toggle. Ideally, once the users flips the toggle I'd like to be able to set a reminder using the date field already entered. I have created an…
BobdeBloke
  • 63
  • 5
2
votes
2 answers

SwiftUI Picker desn't bind with ObservedObject

I'm trying to fill up a Picker with data fetched asynchronously from external API. This is my model: struct AppModel: Identifiable { var id = UUID() var appId: String var appBundleId : String var appName: String var appSKU:…
1
2 3 4 5 6 7