Questions tagged [observableobject]

262 questions
0
votes
2 answers

In SwiftUI, how to implement @Published class in the right way

In SwiftUI, I have a class for user data and I convert JSON data into it: class UserData: Codable { var displayName: String var employeeId: String var firstName: String var lastName: String // more ... } by…
Joe Huang
  • 6,296
  • 7
  • 48
  • 81
0
votes
2 answers

SwiftUI state management

I have encountered an architectural problem with SwiftUI state. Consider an architecture with a uni-directional data flow. class Store: ObservableObject { @Published private(set) var state: AppState = AppState(initialValue: 0) func…
0
votes
1 answer

Fatal error: No ObservableObject of type IconNames found. A View.environmentObject(_:) for IconNames may be missing as an ancestor of this view

I have created a class in SceneDelegate.swift file and I am trying to access it in SettingView.swift and ContentView.swift but I keep getting this error "Fatal error: No ObservableObject of type IconNames found. A View.environmentObject(_:) for…
0
votes
0 answers

Fatal Error using @EnvironmentObject in SwiftUI

import Foundation import SwiftUI import UserNotifications class NotificationHandler: ObservableObject { @Published var notificationCounter = 0 func askPermission() { UNUserNotificationCenter.current().requestAuthorization(options:…
0
votes
2 answers

SwiftUI - Error popup fired in multiple views

Im trying to use MV architecture in my project, below is a simple example of my code: struct PaymentView: View { @StateObject private var store = PaymentStore() var body: some View { NavigationStack { …
OuSS
  • 19
  • 3
0
votes
1 answer

Is it possible to have two init in an ObservableObject class?

I have created an ObservableObject object class, and I need to create 2 inits one with passing a string parameter and another without passing. class DictionaryModel: ObservableObject { @Published var dbName: String = "" private var dbQueue:…
Mc.Lover
  • 4,813
  • 9
  • 46
  • 80
0
votes
1 answer

Keeping the original datasource in sync with downstream bindings

If I have a collection of fruits, and I pass one of them to a detail view, how do I edit that item so that both the item and it's original datasource are updated? final class Merchant: ObservableObject { @Published var selection:…
bobby123uk
  • 892
  • 4
  • 17
0
votes
1 answer

How to create a generic persistent array of ObservableObjects in Swift

I have a class that implements an array of ObservableObjects that will save itself to persistent storage whenever one of the objects changes. Here is a very simplified version of the working code: class MyObject : ObservableObject { @Published…
mstrsn
  • 21
  • 3
0
votes
0 answers

SwiftUI: How wrong is to call the objectWillChange.send() after the change in the model data happened?

I'm trying to better understand the mechanism how SwiftUI gets notified about changes in observed data and invalidates UI. In particular, I wonder if something won't work correctly (e.g. animations?) when objectWillChange.send() is called on…
Tom Kraina
  • 3,569
  • 1
  • 38
  • 58
0
votes
0 answers

Passing data in PreviewProvider SwiftUI, ObservableObject class, ObservedObject

I guys, I have a class with some properties (class ReservationInfo). In my first screen (ReservationFormView) I create an instance of the class(myReservationInfo). I pass the data with a NavigationLink to the second view (ReservationRecapView). It…
0
votes
1 answer

How would I access a published variable from another class?

So I am currently working on my app and have come across a problem where I have to access a published variable in another observable class. Here is some code on what I am trying to do class PeriodViewModel: ObservableObject { @Published var…
GoddlyGut
  • 3
  • 5
0
votes
1 answer

SwiftUI: How could I use polymorphism to make a model class create its own View

I am trying to build a small App in which you can create tests with questions using SwifUI, but finding how to make everything works out is getting hard for a newbie like me. The app would show a list of questions in a main scrollable view and these…
Felix Uff
  • 11
  • 3
0
votes
1 answer

A struct mutating properties or ObservableObject Published properties to drive data changes

I would like help to further understand the implications of using the following 2 methods for driving data between multiple views. My situation: A parent view initialises multiple child views with data passed in. This data is a big object. Each…
meteorBuzz
  • 3,110
  • 5
  • 33
  • 60
0
votes
0 answers

No ObservableObject of type error when calling variable inside struct

I am trying to create a crime tracking app, but right now I am struggling to create structs for different tabs of my app using NavigationManager and screens. However when I made a new struct and tried to use my NavigationManager, it displayed this…
0
votes
1 answer

Passing Data between Delegate and ViewModel ObservableObjects

I have an app built using SwiftUI that works with user location. Using online tutorials, I have come up with a class LocationManager that handles the request using the delegate method and has an attribute @Published that sets the location as soon as…
greatxp117
  • 57
  • 11