Questions tagged [observableobject]

262 questions
1
vote
1 answer

Actor isolate @Observable type

Back in WWDC 2021’s Discover concurrency in SwiftUI, they recommend that you isolate the ObservableObject object to the main actor. E.g.: struct ContentView: View { @StateObject var viewModel = ViewModel() var body: some View { …
Rob
  • 415,655
  • 72
  • 787
  • 1,044
1
vote
0 answers

Trying to get an image view to change on the screen?

I am trying to develop a card game where 8 of the dealer's and player's cards show face down originally. When either the player or dealer are hit or dealt a card, I want to "flip" the card face up to reveal the player's or dealer's face down card. I…
1
vote
1 answer

No ObservableObject of type ModelData found: Missing environmentObject, but the environmentObject is actually there (SwiftUI, Xcode)

Xcode version: 14.2 I am trying to display a list of sports clubs. Since my modelData.clubs is still an empty array when just viewing the preview (modelData.clubs gets its actual value in my contentView, when the app is loaded), I would like it to…
1
vote
1 answer

SwiftUI Pass EnvironmentObject to a class

I'm struggling with passing an variable to a class. I have a class with different settings I store. The data is available and might be changed on different views. class UserData: ObservableObject { @Published var ZipCode = "DK6700" } The…
Lubker
  • 15
  • 5
1
vote
1 answer

ObservableObject with many @Published properties redraws everything unnecessarily

Consider the following code: class Model: ObservableObject { @Published var property1: Int = 0 @Published var property2: Int = 0 } struct ObjectBindingTest: View { @StateObject private var model = Model() var body: some View { …
Morpheus
  • 1,189
  • 2
  • 11
  • 33
1
vote
1 answer

How to use ObservableObject for interlinked properties?

I am trying to convert this working view model to one using ObservableObject from Community Toolkit to reduce boilerplate. public class HslVM : INotifyPropertyChanged { private float hue, saturation, luminosity; private Color color =…
1
vote
1 answer

Nested Observable Objects not updating views on SwiftUI

I've been trying to implement a user class that is called by multiple view to handle various classes to enable exercise and achievement unlocking wherever a certain condition is met. My code currently publishes the xp to all the UI using from the…
Jack1886
  • 37
  • 7
1
vote
0 answers

SwiftUI View is not updating when @Published changed in ObservableObject

I know there are a lot of questions out there with similar issues but almost all of them are reference/value issues. My case is a little different I have a Viewcontroller that leverages UIHostingController and add's a SwiftUI view. I have a…
SpaceDust__
  • 4,844
  • 4
  • 43
  • 82
1
vote
1 answer

Index out of range when updating a @Published var

I am new to Swift/SwiftUI and am trying to build an app that works with the trello API. There is a "TrelloApi" class that is available as an @EnvironmentObject in the entire app. The same class is also used to make API calls. One board is viewed at…
Jan
  • 1,268
  • 4
  • 12
  • 20
1
vote
1 answer

SwiftUI with Core Data and MVVM doesn't update

I am trying to use Core Data with MVVM in SwiftUI, but I can't manage to make the List in my view update whenever there are changes to Core Data. Please check this example. Why my List doesn't update if items is a Published variable? QUESTION: How…
mallow
  • 2,368
  • 2
  • 22
  • 63
1
vote
1 answer

SwiftUI: Pass an ObservableObject's property into another class's initializer

How do I pass a property from an ObservedObject in a View, to another class's initializer in the same View? I get an error with my ObservedObject: Cannot use instance member 'project' within property initializer; property initializers run before…
1
vote
1 answer

Get data from model for multiple List views ObservableObject class and App Storage to save for both list

How to pull in the data from MenuListItemModel.swift ObservableObject classes to load into FriendshipListView.swift and WealthListView.swift. The WealthListView.swift is loading the same data as FriendshipListView.swift. Also, do I have to do…
1
vote
1 answer

How to cancel process() in SwiftUI from different class

I am running a binary file using a swift process() in my DataModel class. The user can see the process running from the ViewTasks() struct. How can I give the user the ability to cancel (terminate) the process? Here is what I have…
1
vote
1 answer

How can I use an ObservableObject as a Binding Target in code and XAML in WinUI 3?

How can I use an ObservableObject (from Community Toolkit) as a Binding Target in code and XAML in a WinUI 3 Desktop app? Here's a simple (partial) homepage example:
1
vote
1 answer

Create array of state objects in SwiftUI

In my project I have a 2D array of my custom class, GridTile representing a grid. This is the class: import Foundation import SwiftUI class GridTile: ObservableObject { @Published var tileNumber = 0 var tileFrame = CGRect.zero var…
JackElia
  • 140
  • 9