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.

173 questions
1
vote
1 answer

How does $ work in SwiftUI and why can I cast a @StateObject to an @ObservedObject

I'm a newbie developing an app which basically has a fridge class, which contains food. I'm trying to have one single fridge with a list of food. So I set foodList to be a published object and instantiate a @StateObject fridge when the program…
TommySun
  • 57
  • 7
1
vote
3 answers

Why @Published updates Text but doesn't update List?

I don't understand why the Text updates but the List does not. I have a model with a @Published which is an array of dates. There's also a method to generate text from the dates. Here's a simplified version: class DataModel: NSObject,…
1
vote
1 answer

Data such email_SignUp does not saves in my ViewModel class even though I used @Published words

Overall, I have a ViewModel which has @Published vars class Parent : ObservableObject{ @Published var email_SignUp = "" @Published var password_SignUp = "" @Published var reEnterPassword = "" } Also, I have a View where user make…
arllashka
  • 11
  • 4
1
vote
1 answer

ObservedObject return nil even if a value is set in first view Swiftui

I have a Class "ActualCourse" class ActualCourse : ObservableObject { @Published var id : UUID? @Published var CourseName : String = "" } And two Structs "Settings" and "Course" struct Settings: View { @State private var action :…
Clément Tengip
  • 618
  • 6
  • 19
1
vote
1 answer

How to change a boolean instance of a class in another view?

I want to tap a button in one view and change the text on another view. The button that changes the bool is on LessonOne and the text I want to show in on my ContentView. The code in my Lesson One is: import SwiftUI import Combine class…
1
vote
2 answers

@StateObject for a NSManagedObject without context not publishing changes

Like in a lot of apps, I have a list of items (populated by a Core Data fetch request), a sheet to create new items, and a sheet to edit an item when tapping on a row in my list. I'm trying to unify both forms to create and edit an update, and put…
alpennec
  • 1,864
  • 3
  • 18
  • 25
1
vote
2 answers

When exactly SwiftUI releases ObservableObjects

I am trying to learn how SwiftUI works internally in terms of memory management. I have little doubt about it. When I add a NavigationLink to the 2nd View which has some Search Functionality and also loading some data from the cloud. Now when I came…
Malav Soni
  • 2,739
  • 1
  • 23
  • 52
1
vote
1 answer

Cannot pass data between views and also update in second view - SwiftUI

I am trying to pass data between 2 views using @Published and @ObservedObject following this article, but for some reason I cannot pass the data through, the @Published just keeps at the initial state that it has been given instead of changing when…
spoax
  • 471
  • 9
  • 29
1
vote
1 answer

How to call a method in my View struct from my ObservableObject class

I am using the @ObservedObject PropertyWrapper for my Picker selection and I want to call a method in my View struct when the selection changes. How can I do that? ObservableObject Code: class SphereModel: ObservableObject { @Published var…
1
vote
1 answer

Passing data with ObservableObject keyword

How can I move data to other screens with ObservableObject keyword? I save the data on the first page to the variable I created with the keyword Published, but I cannot access this data on the second page. User Model import Combine struct User:…
Ufuk Köşker
  • 1,288
  • 8
  • 29
1
vote
1 answer

Value of Selected Option From a SwiftUI Picker does not Update the View

I have the following in a SwiftUI app. Basically I have some settings (Settings class) that I would like to use throughout the app. I have a Settings view that shows a picker to select the value of one of the settings. And other views of the app…
Dogahe
  • 1,380
  • 2
  • 20
  • 50
1
vote
1 answer

SwiftUI ObservedObject not updating across views

I cannot get observable object model to work. I have a simple demo of two views and a view model. The view model is; import Foundation class Score: ObservableObject { @Published var total = 0 } A button view to add one to the total; struct…
Jmcg
  • 239
  • 2
  • 9
1
vote
1 answer

Swift UI DatePicker won't update @ObservedObject value

I am using a class to store a @Published variable but when I try to pass the value chosen on my custom DatePicker to the @ObservedObject in the class I receive the following error: TimePicker(time: self.$time.**timeSelected**) Cannot convert value…
Thom Sides
  • 117
  • 1
  • 7
1
vote
1 answer

How do I ForEach over a One-To-Many Relationship in CoreData using SwiftUI?

I am currently working on a SwiftUI App using CoreData. I do currently have 2 entities defined in CoreData. With Code: extension CoreDataRecipe: Identifiable { @nonobjc public class func fetchRequest() -> NSFetchRequest { …
bob_mosh
  • 267
  • 2
  • 17
1
vote
1 answer

SwiftUI View not updating after @ObservedObject is changed

Intended Feature: Tap to add circle Press "next" to create new "frame" Drag circle to new position Press "back" to revert circle to previous position Issue: As shown above, at the last part when I tap "back", the circle stays at dragged position…
Wilfred
  • 9
  • 1
  • 5